How to use while loops to ask for input again?

closed account (4zUjy60M)
[deleted]
Last edited on
Please format your code correctly. Edit your post and add [code] and [/code] around your code.

1
2
3
4
while(userCmd!=Executive && userCmd!=Manager && userCmd!=Employee) {
  default:
    cout<<"Incorrect choice. Please try again.";
}


This is an infinite loop that you stitched into your switch structure. You're basically doing:
1
2
3
4
while (true)
{
    cout << "Incorrect choice. Please try again.";
}


Rather, you should have the switch-structure inside of the while loop itself.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int userCmd = -1;

do {

    // Get user input (cin)
    cin >> userCmd;

    switch (userCmd)
    {
       case Executive:
         // ...
         break;
       case /*...*/:
         break;
       default:
         cout << "Incorrect choice.\n";
    }

} while (userCmd != Executive && userCmd != Manager && userCmd != Employee);
Last edited on
Topic archived. No new replies allowed.