Loop continues when writing multiple numbers or chars

Greetings everyone!

I'm trying to create a text based game, and I have tried to create a menu with a switch statement, and a while loop.When I type 1 of the numbers, it works fine, but when I write another number that contains 1, 2, 3 or 4, it still runs, like 1344, and the more characters or numbers I write, the more lines of ""INVALID OPTION! Please select a valid menu..." I get. (see 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
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


#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>

using namespace std;

//====================
//      Variables 
//====================
char menuSelect;

int timer(int time) {

}
int health();


//Menus
void menu(){
    cout << "Welcome to TYPE ADVENTURE!" << endl;
    cout << "Please select one of the menus below:" << endl;
    cout << "" << endl;
    cout << "****************" << endl;
    cout << "* 1. Play Game *" << endl << "* 2. Help      *" << endl <<"* 3. Options   *" << endl << "* 4. Exit Game *" << endl << "****************" << endl;
    cin >> menuSelect;
}
void help(){

}

void options(){

}

void exit(){

}



//====================
//      Functions 
//====================

int main()
{


    cout << "==================" << endl;
    cout << "//TYPE ADVENTURE//" << endl;
    cout << "==================" << endl;
    cout << "" << endl;

    menu();

    while(!(menuSelect == '1' || menuSelect == '2' || menuSelect == '3')){
        switch(menuSelect){
         case '1':
            cout << "test\n";
            //Write here
            break;
         case '2':
            cout << "test2\n";
            //Write here
            break;
         case '3':
            cout << "test3\n";
            break;
         default:
            cout << "INVALID OPTION! Please select a valid menu... \n\n";
            cin >> menuSelect;
        }

    }

    cout << "Loop done" << endl;
    return 0;
}
Last edited on
menu();

You need to call menu again inside the loop. Otherwise menuSelect will be the char you first entered forever right?
menu();

You need to call menu again inside the loop. Otherwise menuSelect will be the char you first entered forever right?


I don't want the loop to print the whole menu after a invalid user input, but I tried doing so, but I got the same problems.

Thanks for the answer TarikNeaj
Last edited on
1. I suggest that menu() return the menu value rather than storing it in a global variable.

2. If you want 1234 to be invalid input then you should read a number, not a single character.

3. Do the error checking inside of menu(). In other words, menu() should always return a valid menu choice. If feels awkward, but this is a common case where a loop should exit from the middle. The basic pattern is:
1
2
3
4
5
6
give main prompt
while (true) {
    get input
    if input is valid then break
    print "invalid input" error message
}


Thank dhayden for the answer,

The numbers are now working, thanks. BUT if I enter a char, it runs a infinite loop with the error message.

Last edited on
The problem is now solved, I needed to clear the cin and use the cin.ignore, after the invalid message.

Thanks everyone
Topic archived. No new replies allowed.