Switch case looping

Ok so I have a menu program here which works fine. I'm trying to account for error input by the user. I'm unsure how I let the user enter an input again if they accidently made a mistake. I know I have to use loops.

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
  // Example program
#include <iostream>
#include <string>

using namespace std;

void play_movie()
{
    cout << "you are now playing!";
}

void pause_movie()
{
    cout << "you have now paused the movie!";
}

void rewind_movie()
{
    cout << "you have now rewinded the movie!";
}

void forward_movie()
{
    cout << "You have now forwarded the movie!";
}



int main()
{
    int input;
    cout << "1) Play movie \n";
    cout << "2) Pause movie \n";
    cout << "3) Rewind movie \n";
    cout << "4) Forward movie \n";
    cout << "5) What do you want to do: ";
    cin >> input;
    switch (input)
    {
        case 1:
            play_movie();
            break;
        case  2:
            pause_movie();
            break;
        case 3:
            rewind_movie();
            break;
        case 4:
            forward_movie();
            break;
        default:
            cout << "Invalid Input";  
            
    }










        
  
  
}
@kvilla32

You could try something like this...
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
int input;
do
{
    cout << "1) Play movie \n";
    cout << "2) Pause movie \n";
    cout << "3) Rewind movie \n";
    cout << "4) Forward movie \n";
    cout << "5) Quit the program: \n\n";
   cout << "What do you want to do: \n";

    cin >> input;
    switch (input)
    {
        case 1:
            play_movie();
            break;
        case  2:
            pause_movie();
            break;
        case 3:
            rewind_movie();
            break;
        case 4:
            forward_movie();
            break;
        case 5:
           cout << "The program has now been terminated. Bye.. \n";
           break;
        default:
            cout << "Invalid Input\n\n";  
      }
}while(input !=5);
Last edited on
I would do something like this.

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
  // Example program
#include <iostream>
#include <string>

using namespace std;

void play_movie()
{
    cout << "you are now playing!";
}

void pause_movie()
{
    cout << "you have now paused the movie!";
}

void rewind_movie()
{
    cout << "you have now rewinded the movie!";
}

void forward_movie()
{
    cout << "You have now forwarded the movie!";
}



int main()
{

    int input;
    
do
{
    cout << "1) Play movie \n";
    cout << "2) Pause movie \n";
    cout << "3) Rewind movie \n";
    cout << "4) Forward movie \n";
    cout << "5) What do you want to do: ";
    cin >> input;

    switch (input)
    {
        case 1:
            play_movie();
            break;
        case  2:
            pause_movie();
            break;
        case 3:
            rewind_movie();
            break;
        case 4:
            forward_movie();
            break;
        default:
            cout << "Invalid Input" << endl;  
            
    }
}  
while((input > 4) || (input < 1));

	 cout << endl << endl;
 system ("PAUSE");
 
 return 0;









        
  
  
}
The pattern that I use is like this:
1
2
3
4
5
6
7
while (true) {
    prompt for input
    if (input is valid) {
        break;
    }
    print message saying input is invalid and they should enter it again.
}


This may seem awkward since the loop test is in the middle of the loop, but it really does express what's going on.
Topic archived. No new replies allowed.