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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>

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 <<endl << endl;

  system("pause");
  return 0;
}


Maybe you meant instead of

1
2
        if (order == Q)
        break;

the following condition

1
2
        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?
and your code after added:

 
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>

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 <<endl << endl;

  system("pause");
  return 0;
}
...
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.