Loops forever with invalid input

ok i want it to say this input is not valid when you write anything besides 0,1,2, or 3 but
1
2
3
4
5
if (menu != 1 && menu != 2 && menu != 3 && menu != 0)
		{
			cout << "This is not a valid Input" << endl;
			cin >> menu;
		}

is messed up? i dont know
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
int main()
{
	system("color C8");

	while (menu == 1||menu == 2||menu == 3||menu == 0)
	{
		cout << "Welcome to TeacherHelp" << endl;
		cout << "This Program will do many things to help teachers." << endl;
		cout << "Write the number of your choice." << endl;
		cout << "[1]Calculator" << endl;
		cout << "[2]Average Grades" << endl;
		cout << "[3]Note" << endl;
		cout << note << endl;
		cin >> menu;
		std::cin.ignore();
		if (menu != 1 && menu != 2 && menu != 3 && menu != 0)
		{
			cout << "This is not a valid Input" << endl;
			cin >> menu;
		}
			switch(menu)
			{
			case 1:
				int calculator();
				break;
			case 2:
				int grades();
				break;
			case 3:
				notes();
				break;
			}
		}
	return 2182;
}
Your two conditionals are different, the while loop will certainly loop forever.

As a side, you can work your switch better, forget about the if statement.

1
2
3
4
5
6
7
8
switch(menu)
{
case 1: cout << "You entered one"; break;
case 2: cout << "You entered two"; break;
case 3: cout << "You entered three"; break;
case 4: cout << "You entered four"; break;
default: cout << "You entered something I don't know about";
}


If the user hits anything other than 1-4, you get the default case.
Last edited on
this is good except if they enter something wrong twice it closes
Last edited on
I don't mean to be rude, just look at the difference between the two and figure out why they don't work the same. I can tell you the answer or perhaps you learn something.
i get it now i have contradicting things i said if menu =1 or menu = 2 or menu=3 or menu = 4 do this but then i said if its not ask for menu again so itll keep going
Topic archived. No new replies allowed.