Craig6682
Member
Registered: 8th Apr 03
User status: Offline
|
Here's the code:
#include <stdio.h>
#include <conio.h>
main ()
{
int sales_per_num,
quarter,
amount ;
printf ("To end this program at any point and see the sales analysis please enter a NEGATIVE value !!!\n\n") ;
while (sales_per_num != "X")
{
printf ("\aPlease enter the Sales Person's Numer: ") ;
scanf ("%d", &sales_per_num) ;
printf ("\aPlease enter the Quarter: ") ;
scanf ("%d", &quarter) ;
printf ("\aPlease enter the Sales Amount: ") ;
scanf ("%d", &amount) ;
printf ("Sales Person's Number: %d \n", sales_per_num) ;
printf ("Quarter: %d \n", quarter) ;
printf ("Sales Amount: %d \n", amount) ;
}
getch () ;
}
Why when i press the letter X does it not end the program?
|
combatprojex
Member
Registered: 12th Oct 03
Location: Wellingborough Drives: Corsa 1.2 16v & proud of it
User status: Offline
|
x on the window or type the letter x??
|
Craig6682
Member
Registered: 8th Apr 03
User status: Offline
|
Type in the letter X
|
Tom J
Organiser: South Wales Premium Member
Registered: 8th Sep 03
Location: Bridgend
User status: Offline
|
have u defined X as a negative value
|
cdcool1
Member
Registered: 9th Jun 02
Location: Scunny
User status: Offline
|
i've not done C, but done a bit of C++, shouldn't it be
do
{
printf ("\aPlease enter the Sales Person's Numer: ") ;
scanf ("%d", &sales_per_num) ;
printf ("\aPlease enter the Quarter: ") ;
scanf ("%d", &quarter) ;
printf ("\aPlease enter the Sales Amount: ") ;
scanf ("%d", &amount) ;
printf ("Sales Person's Number: %d \n", sales_per_num) ;
printf ("Quarter: %d \n", quarter) ;
printf ("Sales Amount: %d \n", amount) ;
}
while (sales_per_num != "X")
also, are you pressing X and not x?
|
combatprojex
Member
Registered: 12th Oct 03
Location: Wellingborough Drives: Corsa 1.2 16v & proud of it
User status: Offline
|
because it the value for sales_per_num... what programme u usin?
|
Craig6682
Member
Registered: 8th Apr 03
User status: Offline
|
Borland C++
|
Paul_J
Member
Registered: 6th Jun 02
Location: London
User status: Offline
|
errr - perhaps coz it says 'while it is not equal to X'
so it keeps doing it till you enter X... but you've set the variable as a interger - so it can never be 'X' as thats a character
|
cdcool1
Member
Registered: 9th Jun 02
Location: Scunny
User status: Offline
|
oh yea, you haven't said that the character you are getting in from the keyboard will be sales_per_num
|
Craig6682
Member
Registered: 8th Apr 03
User status: Offline
|
quote: Originally posted by cdcool1
i've not done C, but done a bit of C++, shouldn't it be
do
{
printf ("\aPlease enter the Sales Person's Numer: ") ;
scanf ("%d", &sales_per_num) ;
printf ("\aPlease enter the Quarter: ") ;
scanf ("%d", &quarter) ;
printf ("\aPlease enter the Sales Amount: ") ;
scanf ("%d", &amount) ;
printf ("Sales Person's Number: %d \n", sales_per_num) ;
printf ("Quarter: %d \n", quarter) ;
printf ("Sales Amount: %d \n", amount) ;
}
while (sales_per_num != "X")
also, are you pressing X and not x?
fecks up wen u do both !!! It jst keeps looping
|
cdcool1
Member
Registered: 9th Jun 02
Location: Scunny
User status: Offline
|
quote: Originally posted by Paul_J
errr - perhaps coz it says 'while it is not equal to X'
so it keeps doing it till you enter X... but you've set the variable as a interger - so it can never be 'X' as thats a character
i went straight passed that
|
Craig6682
Member
Registered: 8th Apr 03
User status: Offline
|
quote: Originally posted by Paul_J
errr - perhaps coz it says 'while it is not equal to X'
so it keeps doing it till you enter X... but you've set the variable as a interger - so it can never be 'X' as thats a character
Never thought of that !!!!
|
Paul_J
Member
Registered: 6th Jun 02
Location: London
User status: Offline
|
try changing it to - while (sales_per_num !< 0)
while its not less than 0 it keeps repeating.. in your code u said simply enter a negative number to exit.
this will mean you can keep the variable as a int and just enter -1 or whatever to get it to exit.
|
Craig6682
Member
Registered: 8th Apr 03
User status: Offline
|
quote: Originally posted by Boyrac3r
quote: Originally posted by Paul_J
errr - perhaps coz it says 'while it is not equal to X'
so it keeps doing it till you enter X... but you've set the variable as a interger - so it can never be 'X' as thats a character
Never thought of that !!!!
It works !!! Niceone all
|
Paul_J
Member
Registered: 6th Jun 02
Location: London
User status: Offline
|
I also only do C++, never done C, so don't know exactly what to write for some of these things... though I've got a bloody big assignment in soon for C++ which I was supposed to write over christmas and haven't even looked at oh shit.
|
Paul_J
Member
Registered: 6th Jun 02
Location: London
User status: Offline
|
hehe - no prob, at college when we do coding, we all end up getting stuck - sometimes helps to have others just quickly check your code.
See loads of people do the same sorta things.
hey, I do some really stupid things some times - I don't like to plan programs first, I just like to write them - end up with X's, Z's and A's and B's all over the place and when I get a prob it can be a bitch to find out the cause - usually ends up being something simple like that though
|
Craig6682
Member
Registered: 8th Apr 03
User status: Offline
|
C is more or less the same as C++, or so i'm told.
It does help to ask your class mates if you get stuck, i've found that out. Only problem is - is that it's Christmas and we don't go back until the 19th i think and this course-work is due in on that day. Got two weeks to do it like but though i'd get it done now as i know when i'm back at work i'll be to fecked to do it and just leave it until the last minute.
The hard part is done now anyways. Just gotta tidy up the code, add comments and then do the report. I should get most of it done tonight and tomorrow night.
Thanks for all you help !!!
|
Ian
Site Administrator
Registered: 28th Aug 99
Location: Liverpool
User status: Offline
|
Why did you ask the user to type in a negative value then check for a character that was declared as an integer anyway?
Very strange!
|
Craig6682
Member
Registered: 8th Apr 03
User status: Offline
|
Changed my mind what i wanted to do
|