### Having some issues with an IF statement within a WHILE statement

My code was working fine before placing an IF statement within a WHILE statement (see line 31). You should be able to enter "Q" when all orders are entered and it stops data entry. It then needs to count and display the number of each combo ordered and calculate and display the total group bill. Any help would be appreciated.

Here's the code:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` ``````#include using namespace std; int main () { double costA = 6.00; double costB = 6.25; double costC = 5.75; double costT = 0.0; char answer = ' '; char order = ' '; int numCombo = 0; //counter cout <<"Is this a group order? "; cin >> order; order = toupper(order); cout << endl; cout << "Combination Menu:" << endl << endl; cout << "Combo A: Fried chicken with cold slaw [price: 6.00]" << endl; cout << "Combo B: Roast beef with mashed potato [price: 6.25]" << endl; cout << "Combo C: Fish and chips [price: 5.75]" << endl << endl; while (order == 'Y') { cout <<"Enter combo letter [A/B/C/], [Enter T for total and Q to stop entries]: "; cin >> answer; answer = toupper(answer); if (order == Q) break; numCombo = numCombo + 1; switch (answer) { case 'A': costT = costT + costA; break; case 'B': costT = costT + costB; break; case 'C': costT = costT + costC; break; default: cout << "Data entry complete" << endl; } } cout << endl; cout << "Number of combo A ordered: " << numCombo << endl; cout << "Number of combo B ordered: " << numCombo << endl; cout << "Number of combo C ordered: " << numCombo << endl; cout << "Total amount due: " << costT <

 ``12`` `````` if (order == Q) break;``````

the following condition

 ``12`` `````` if (order == 'Q') break;``````
`if (answer == 'Q')`

I think this is what you were trying to do. There are other problems though. Inputting 't' doesn't give total and 'numcombo' doesn't keep count of how many orders of each combo, it keeps count how many times the program has looped.

Add another switch case for 't' and I think each combo will need its own counter variable.
Last edited on
Yes, that does it, but now it won't provide the total if "T" is entered. It just stops the loop completely. Any suggestions?

 `` `` ``case 'T'``

is...?
Thanks all! Everything seems to working as it should EXCEPT the "numCombo" counts (lines 34-36). I'm also not sure about "Q" either. It does stop the data entry but it also calculates the total which shouldn't be the case. Any suggestions would be appreciated.

Here's my latest code:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````#include using namespace std; int main () { double costA = 6.00; double costB = 6.25; double costC = 5.75; double costT = 0.0; char answer = ' '; char order = ' '; int numComboA = 0; //counter int numComboB = 0; //counter int numComboC = 0; //counter cout <<"Is this a group order? "; cin >> order; order = toupper(order); cout << endl; cout << "Combination Menu:" << endl << endl; cout << "Combo A: Fried chicken with cold slaw [price: 6.00]" << endl; cout << "Combo B: Roast beef with mashed potato [price: 6.25]" << endl; cout << "Combo C: Fish and chips [price: 5.75]" << endl << endl; while (order == 'Y') { cout <<"Enter combo letter [A/B/C/], [Enter T for total and Q to stop entries]: "; cin >> answer; answer = toupper(answer); numComboA = numComboA + 1; numComboB = numComboB + 1; numComboC = numComboC + 1; switch (answer) { case 'A': costT = costT + costA; break; case 'B': costT = costT + costB; break; case 'C': costT = costT + costC; break; default: order = 'Q'; } } cout << endl; cout <<"Number of combo A ordered: " << numComboA << endl; cout <<"Number of combo B ordered: " << numComboB << endl; cout <<"Number of combo C ordered: " << numComboC << endl << endl; cout <<"Total amount due: \$ " << costT <
...
Last edited on
Your 3 lines (34-36) are all executing every time you go through the while loop. You want to add them to the part of the loop that is only executed based on the input answer.

Hopefully that helps.
Thanks, everyone! I was able to solve the problem I had with this program. :-)
Topic archived. No new replies allowed.