Switch Case - Problem Going Insane

Hey all, I cannot figure out what is wrong with my switch case. Case 1 seems to be working. But case 2 and others aren't.

Here is 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

#include <iostream>

using namespace std;

int main()
{
	int menu;
	cout << "MENU" << endl;
	cout << "1- INFINITE HEALTH" << endl;
	cout << "2- INFINITE AMMO" << endl;
	cout << "3- INFINITE SHIT" << endl;
	cin >> menu;
	
	switch(menu)
	{
	case 1:
     cout << "INIFNITE HEALTH" << endl;
	 break;

	case 2:
		cout << "MORE AMMO" << endl;
		break;

	default:
		cout << "WRONG INPUT" << endl;

	}
	system("PAUSE");
}
That code works fine; http://cpp.sh/66gvh
Yep works fine here too https://onlinegdb.com/rJdrhFBvm

May I know what command do you use to compile, and does your code throw any errors, or just doesn't work?

Also, always avoid system(). Better use something like this:
1
2
string notImportant;
getline(cin, notImportant);

(You will need to add #include <string> at the begginning of your file)

And end your main() with a return 0
Last edited on
And end your main()with a return 0

Not necessary but it doesn't hurt.
ponasm


I am just using debug x64. It compiles with no error but when I press number 2 on keyboard it will not display. Only case 1 works.

Also why do I need to use <string> I am not using anything as a string?
when I press number 2 on keyboard it will not display


I don't see this problem, you did run the program again didn't you? run it and go straight for option 2, i expect you will get the same results as the rest of us.

your program will only accept one input before quitting, you may be aware of this but it is the "beginners" section so i choose to err on the side of caution :)

ponasM's suggestion is that you replace the system("xx") call with something that takes input from the user and discards it, that will stop your program and you can hit return to let it finish and exit.
you could equally say std::cin.get();

the cut and thrust of this is that system("") should be avoided, its really for running other programs or DOS box commands.
Last edited on
Topic archived. No new replies allowed.