I want to keep printing menu function if user enters wrong number

My code works perfectly I just want to know how I can modify my code so if the user enters anything other than the numbers 1, 2, 3, or 4 the program will print the menu again asking for the user to select an option. Or even maybe write "you have entered an incorrect option, please choose again."


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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <stdlib.h>

using namespace std;

void print_menu();
void reverse();
void matrix();
void palindrome();

int main ()
{
    int option;    
        
     do
    {
        print_menu();
        cin >> option;
        cout << endl;
        if (option == 1)
        {
            reverse();
        }    
        else if (option == 2)
        {
            matrix();
        }
        else if (option == 3)
        {
            palindrome();
        }
        else if (option == 4)
        {    
            cout << "Your program will exit.\n";
            break;
        }
        
    } while (option <=4 && option >=1);
system ("Pause");
return 0;
}


void print_menu()
{
    cout << "Please read the menu below and choose from one of the following options by pressing 1, 2, 3, or 4:\n";
    cout << "1. Given an input a String and output its reverse .\n";
    cout << "2. Given an Integer n and an input character C, print an n x2n matrix of C's .\n"; 
    cout << "3. See if the input string is a palindrome.\n";
    cout << "4. Exit program.\n";
    cout << endl;
}

void reverse()
{
    string word;    
    cout <<"Enter string.\n";
    cin >> word;
    int length = word.length();    
    for (int i = length-1; i >= 0; i--)
    { 
        cout << word[i]<<endl;
    }
}

void matrix ()
{
    int value;    
    char letter;
    cout << "Input an integer value: ";
    cin >> value;
    cout << "Enter any character: ";
    cin >> letter;
    
    for ( int row = 0; row < value*2; row++)
    {    
      for ( int col = 0; col < value; col++)
    {
        cout << letter;         
    }
        cout << endl;
    }
}

void palindrome ()
{
    string word;    
    cout <<"Enter string.\n";
    cin >> word;
    int end = word.length();
    int mid = end/2;
    end = end - 1;
    int start = 0;
    while (mid > start)
    {
        if (word[start] == word[end])
        {
           end--;
          start++; 
        }
        else
        {
            cout << "This is not a palindrome.\n";
        break;
        }
    }
    if (word[start] == word[end])
    {
         cout << "This is a palindrome!\n";    
    }
}
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main(){

	int num;
	std::cout << "Enter 1 - 4: ";
	std::cin >> num;

	while (num <= 0 || num > 4){
		std::cout << "\nInvalid Choice. Enter 1 - 4: ";
		std::cin >> num;
	}

	std::cout << "Exit\n";

	return 0;
}


http://www.cplusplus.com/forum/beginner/145243/
Last edited on
A switch statement would be more appropriate here.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool invalidChoice = true;

while(invalidChoice)
{
    int choice = 0;
    switch(choice)
    {
         case 1:
             //do something
             break;
         //other cases
        default:
            std::cout << "Invalid Choice!\n";
    }
}
Last edited on
Whenever I want to keep something running in main, I put everything in a never ending while loop:

1
2
3
4
5
6
7
8
9
10
int main()
{
    while(1)
    {
        switch(GetInput())
        {
            case EXIT: return 0;
        }
    }
}


EDIT:

After observing your code the fix is trivial:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
print_menu();
        cin >> option;
        cout << endl;
        if (option == 1)
        {
            reverse();
        }    
        else if (option == 2)
        {
            matrix();
        }
        else if (option == 3)
        {
            palindrome();
        }
        else if (option == 4)
        {    
            cout << "Your program will exit.\n";
            break;
        }
        else cout << "Invalid choice\n";
Last edited on
Thanks everyone for your help!
I did it this way and it works but is it too wordy?
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
int main ()
{
    int option;    
     print_menu();
     cin >> option;
     cout << endl;   
     do
    {
        
        
        if (option == 1)
        {
            reverse();
            print_menu();
            cin >> option;
            cout << endl;
        }    
        else if (option == 2)
        {
            matrix();
            print_menu();
            cin >> option;
            cout << endl;
        }
        else if (option == 3)
        {
            palindrome();
            print_menu();
            cin >> option;
            cout << endl;
        }
        else if (option == 4)
        {    
            cout << "Your program will exit.\n";
            break;
        }
        else if (option > 4 || option < 1) 
        {
            cout <<"Invalid choice, please choose again.\n";
            cout << endl; 
            print_menu();
            cin >> option;
            cout << endl;
                         
        }    
               
    } while (option <=4 && option >=1);
system ("Pause");
return 0;
}
Last edited on
You could use a switch as renthalkx97 suggested.
Topic archived. No new replies allowed.