switch statment looping non-stop

Hello C++ members,

I'm having a tough time with this switch statement of mine. You see when someone presses a letter instead of a number the program flips out and begins to loop non-stop. Hopefully someone has a simple solution for my problem.

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 Menu::inti()
{
	int select;
	cout << "welcome to the game!\n\n";

	cout << "Please choose from the menu\n";
	cout << "1.Login\n";
	cout << "2.Create a account\n";
	cin >> select;

	switch(select)
	{
	case 1:
		login.welcome(1);
		login.login();
		login.checkLogin();
		stats.displayStats(2);
		break;
	case 2:
		login.welcome(0);
		login.login();
		login.createLogin();
		stats.firstTime(1);
		break;
	default:
		system("CLS");
		cout << "error, try again\n";
		return inti();
	}

	return gameMenu();
}


again this works perfectly when I use numbers. Not so much with letters.

Thanks,
-Jolt
How about you start by replacing that recursive call with a loop?
What's happening is what always happens when someone uses cin: the first time you enter something a '\n' is left at the beginning of the input buffer. This '\n' is read the next time there's cin is called. This is interpreted as an empty input, so select is not assigned. Perhaps it wouldn't be a bad idea to initialize select to something. 0 is not a bad choice.

http://www.cplusplus.com/forum/articles/6046/
Topic archived. No new replies allowed.