foor loop won't stop looping

I'm trying to write code for a menu, that continues to ask for options until you type 'q' for quit. It seemed easy enough, but for some reason only one of my if loops refuses to stop looping even if "continue;" is present. The only way I can get it to stop looping is by using "break;" but then it breaks out of the while loop and I can't keep the menu open. I've tried moving the input before and after inside and outside the for loops to see if that helps, but nothing I try seems to help.

Also, I should mention that it's the ADD if loop that isn't working. The other loops seem to be working fine. Check for case 'a'.

Also, I've tried doing a switch statement as well, but the case 'a' still is the only one that won't stop repeating. Every other case operates once, then breaks and takes in another menu input like it should. Case 'q' ends the menu like it should. It really is just the case 'a' but I can't figure out why.

Does anyone know what I'm doing wrong? I know it must be something silly...

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
   do {
      cin >> input;

      if (input == ' ') {
         continue;
      }

      if (input == 'a') {
         cout << "ADD ITEM TO CART" << endl;

         cout << "Enter the item name:" << endl;
         cin >> item_name;
         cout << "Enter the item description:" << endl;
         cin >> descript;
         cout << "Enter the item price:" << endl;
         cin >> price;
         cout << "Enter the item quantity:" << endl;
         cin >> qty;

         item.SetDescription(item_name, descript, price, qty);

         cout << endl;
         cout << "Choose another option:" << endl;
         continue;
      }

      if (input == 'd') {
         cout << "REMOVE ITEM FROM CART" << endl;

         cout << "Enter name of item to remove:" << endl;
         getline(cin, item_name);

         cout << endl;
         cout << "Choose another option:" << endl;
         continue;
      }

      else if (input == 'c') {
         cout << "CHANGE ITEM QUANTITY" << endl;

         cout << "Enter the item name:" << endl;
         getline(cin, item_name);
         cout << "Enter the new quantity:" << endl;
         cin >> qty;

         descript = "none";
         price = 0.00;

         itemChange.SetDescription(item_name, descript, price, qty);
         currCustomer.ModifyItem(itemChange);

         cout << endl;
         cout << "Choose another option:" << endl;
         continue;
      }      

      else if (input == 'i') {
         currCustomer.PrintDescriptions();
         cout << endl;
         cout << "Choose another option:" << endl;
         continue;
      }      

      else if (input == 'o') {
         cout << "OUTPUT SHOPPING CART" << endl;
         currCustomer.PrintTotal();
         cout << endl;
         cout << "Choose another option:" << endl;
         continue;

      }

      else if (input == 'q') {
         continue;
      }

      else {
         cout << "Invalid option. Please try again:" << endl;
         cin >> input;
         continue;
      }

      input = ' ';

   } while (input != 'q');


or alternatively, the switch statement:

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
   do {
      cin >> input;
      
      switch (input) {
      
      case ' ':
         break;
         
      case 'a':
         cout << "ADD ITEM TO CART" << endl;
         
         cout << "Enter the item name:" << endl;
         cin >> item_name;
         cout << "Enter the item description:" << endl;
         cin >> descript;
         cout << "Enter the item price:" << endl;
         cin >> price;
         cout << "Enter the item quantity:" << endl;
         cin >> qty;
         
         item.SetDescription(item_name, descript, price, qty);
         
         cout << endl;
         cout << "Choose another option:" << endl;
         break;
      
      case 'd':
         cout << "REMOVE ITEM FROM CART" << endl;
         
         cout << "Enter name of item to remove:" << endl;
         getline(cin, item_name);
         
         cout << endl;
         cout << "Choose another option:" << endl;
         break;
      
      case 'c':
         cout << "CHANGE ITEM QUANTITY" << endl;
         
         cout << "Enter the item name:" << endl;
         getline(cin, item_name);
         cout << "Enter the new quantity:" << endl;
         cin >> qty;
         
         descript = "none";
         price = 0.00;
         
         itemChange.SetDescription(item_name, descript, price, qty);
         currCustomer.ModifyItem(itemChange);
         
         cout << endl;
         cout << "Choose another option:" << endl;
         break;    
      
      case 'i':
         currCustomer.PrintDescriptions();
         cout << endl;
         cout << "Choose another option:" << endl;
         break;    
      
      case 'o':
         cout << "OUTPUT SHOPPING CART" << endl;
         currCustomer.PrintTotal();
         cout << endl;
         cout << "Choose another option:" << endl;
         break;
         
      case 'q':
         break;
   
      default:
         cout << "Invalid option. Please try again:" << endl;
         cin >> input;
         break;
      
      input = ' ';
      
      }
      
   } while (input != 'q');
Last edited on
Hello grae94,

what you have posted tells part of the code. What it does not tell me is how "input" is defined.

Of the two I like the switch the best. Also in the case statements use this:
1
2
3
4
case 'a':
case 'A':
  // code
  break;

Use this for the letters. DO NOT count on the user to type the proper case.

With out the rest of the code there is no way to test this without a lot of work.

Andy
Hello grae94,

in line 83 or 76 you set "input" to a space which is not equal to 'q', so you are setting yourself up to make it an endless loop. Either remove line 83 or 76 or put it at the top of the loop as input = '\0' as long as "input" is defined as a "char". Although putting it at the top of the loop really does not help as the first thing you would is a "cin" statement.

Andy
@grae94

This does what you want, more or less.

I've trimmed out all the non-loop functional stuff. All you have to do is put it back.

You've committed one of the 7 deadly sins and combined functions with trying to get the interface loop to work. The best thing to do when that happens is to extract the junk and concentrate on the problem at hand - i.e. getting the loop to do what you want and then feeding that problem solved into the bigger picture.

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
#include <iostream>

using namespace std;

int main()
{
    char input;
    
    while (cout << "Pls enter a letter: " && cin >> input && input !='q')
    {
        switch (input)
        {
            case ' ':
                break;
                
            case 'a':
                cout << "ADD ITEM TO CART" << endl;
                break;
                
            case 'd':
                cout << "REMOVE ITEM FROM CART" << endl;
                break;
                
            case 'c':
                cout << "CHANGE ITEM QUANTITY" << endl;
                break;
                
            case 'i':
                cout << "Choose another option:" << endl;
                break;
                
            case 'o':
                cout << "OUTPUT SHOPPING CART" << endl;
                break;
            
            default:
                cout << "Invalid option. Please try again:" << endl;
                break;
        }
        
    }
    
    return 0;
    
}
BTW use the toupper() or tolower() function to allow for upper vs lower case. i.e doing the conversion saves OR's and/or fiddly switch cases
Topic archived. No new replies allowed.