• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ programming help

saahmed

Golden Member
I basically have the majority of the program written out. But, for some reason the while statement will not end when the sentinel value (variable sentinelMeal) has been reached. Could somebody help me out with this? Do I need to take the 'meals=meals+quantity' out of the switch statement?

I am not getting any errors or warnings, so I think mostly everything is fine.

Thanks, any help is appreciated.
 
Originally posted by: Spydermag68
While (x < y)
{
...
...
x++;

}


sorry doesnt help. I already have that though it is

while(meals!=sentinelMeal)
{

meals=meals+quantity;
}

but its not working.
 
Is meals ever greater than sentinelMeal? Something to think about...!= only checks for inequality. Only if meals just hit the limit will it stop, for if it's greater than the limit it will keep going. Change != to <=.
 
int to double comparisons often cause problems.

meals will be 4 while sentinelMeal will be 4.00000000000124 or 3.9999999987 or similar.

Genrally you should either
a) allow a small range to stand for equality, e.g. instead of != you would check that the abs() value of the difference is less than (small amout)
b) cast the double to int after adding (small amount)
 
I recommend making sentinelMeal a double so it corresponds with the current double-typed total you have there.
 
Originally posted by: xtknight
Is meals ever greater than sentinelMeal? Something to think about...!= only checks for inequality. Only if meals just hit the limit will it stop, for if it's greater than the limit it will keep going. Change != to <=.


Hmm...thought that might work, but just tried it, and still doesnt end while statement.
 
Originally posted by: xtknight
I recommend making sentinelMeal a double so it corresponds with the current double-typed total you have there.


that makes it skip the whole while statement and gives me a garbage value for total at the bottom.
 
Originally posted by: xtknight
I recommend making sentinelMeal a double so it corresponds with the current double-typed total you have there.
That's often not good enough for == and != comparisons either, since you still can have something like 1.0 != 1.000000000000002 after a series of math operations.
 
Originally posted by: DaveSimmons
Originally posted by: xtknight
I recommend making sentinelMeal a double so it corresponds with the current double-typed total you have there.
That's often not good enough for == and != comparisons either, since you still can have something like 1.0 != 1.000000000000002 after a series of math operations.


I changed the != to <=, shouldnt that work? But, it still is not.
 
If you changed sentinel to a double did you also fix the scanf?

Also, it's more natural to have meals, sentinel, quantity as integer since they all are.

You can cast quantity to double when calculating price, or let the compiler do it automatically.
 
I made a couple of changes. I dont think my switch statement is working. I added a default to the switch, but when I enter like 12, it simply keeps on asking for combo number and quantity number.
 
Originally posted by: DaveSimmons
If you changed sentinel to a double did you also fix the scanf?

Also, it's more natural to have meals, sentinel, quantity as integer since they all are.

You can cast quantity to double when calculating price, or let the compiler do it automatically.


What would I have to change in the scanf?
 
I think I am seeing a problem with my switch statement. No matter what combo number I enter, it goes to default case. Did I set up the switch statement correctly? I think that is wher the problem lies.
 
I did a printf statement after the switch statment. printf("the combo number you enter was %d, combo");

When I entered 3, the printf statement said I had entered 1916. Any idea why that might be happening?
 
Originally posted by: saahmed
What would I have to change in the scanf?
scanf("%d",&sentinelMeal);
%d is not usable with all data types.

think I am seeing a problem with my switch statement.
is '1' the same as 1 ?

printf("the combo number you enter was %d, combo");
look up printf in your docs, and notice where you put the closing quoute.

 
Originally posted by: DaveSimmons
Originally posted by: saahmed
What would I have to change in the scanf?
scanf("%d",&sentinelMeal);
%d is not usable with all data types.

think I am seeing a problem with my switch statement.
is '1' the same as 1 ?

printf("the combo number you enter was %d, combo");
look up printf in your docs, and notice where you put the closing quoute.


do I need to change the "%d" to something else for the doubles?

And I just thought you had to have the single quote around those values, should I take them out?

For some reason, all my number are coming up as 1916 in the printf.
 
Originally posted by: DaveSimmons
Originally posted by: saahmed
What would I have to change in the scanf?
scanf("%d",&sentinelMeal);
%d is not usable with all data types.

think I am seeing a problem with my switch statement.
is '1' the same as 1 ?

[Q] printf("the combo number you enter was %d, combo");
look up printf in your docs, and notice where you put the closing quoute.

[/quote]

Oh, yeah, duh! Okay combo values going in correctly. But still not working.
 
'1' is a char / character code and converts to an int based on its ASCII value of 49.

int x = '1' ; // = 49


look up scanf codes in your docs to see why %d works better for ints than doubles or chars.
 
Okay, I think I got some things straightened out. What variables should I use int, and which ones double? It is not stopping at the sentinel value, but like one higher, probably having to do with the double issue.

And my total is coming out as a large negative number.
 
Originally posted by: DaveSimmons
If you changed sentinel to a double did you also fix the scanf?

Also, it's more natural to have meals, sentinel, quantity as integer since they all are.

You can cast quantity to double when calculating price, or let the compiler do it automatically.
here you go, and use some printfs to check all your scanfs are working properly.

 
Okay, almost fixed. I am getting a correct total and everything, but the while statement still will not end at the sentinelMeal. I think, like you were mentioning earlier, DaveSimmons, they are adding up to just below the sentinel value, like .94 or something. I have to add one extra.

I did change all those variables to int, but still having this problem, what else could be wrong.

BTW, thanks for all your help!
 
Originally posted by: xtknight
needs to be a space in here maybe:

change
case'4':
to
case '4': ?


Actually, DaveSimmons taught me that using the single quotes converts integers to their ASCII value. So I took those out.
 
Add a printf just before the end of your while to see the values of meals and sentinel after each time through the code.

Note that for int / double and double / double comparison the small value will usually only need to be very small, like 0.00005.

if ( abs( mydec - myint ) < 0.00005 ) // ==

if ( mycount + 0.00005 <= mymax )
 
Back
Top