do...while loop requires me two input twice for it to work

Hello everyone. So basically I was having a lot of problems with this loop. This is a program that should help me choose the right decision when playing blackjack. I created a small menu where the user can choose what he/she wishes to do. However, when attempting to create a loop that goes back to the menu after the user is done "playing", the loop just wouldn't stop. I checked out replies to similar problems on the forum and added the cin.clear(); and cin.ignore(numeric_limits<streamsize>::max(), '\n'); you can see there. The loop finally stopped, but now I need to type my choice in twice for it to work. Any help on how to make it so that one input is enough is highly appreciated!

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
  void main()
{
	unsigned int menuChoice;
	int languageChoice;
	bool Quit = false;
	
	displayMenu();
	cin >> menuChoice;

	do{
		
		

		

		switch(menuChoice){
			case 1:
			{
				bestPlay();
				displayOptions();
				//cin >> menuChoice;
				cin.clear();
				cin.ignore(numeric_limits<streamsize>::max(), '\n');
				cin >> menuChoice;
				break;
			}
			case 2:
			{
				displayInstructions();
				bestPlay();
				displayOptions();
				break;
			}
			case 3:
				displayLanguage();
				cin >> languageChoice;
				switch (languageChoice) {
					case 1: 
					{
						bestPlay();
						displayOptions();
						break;
					}
					case 2: 
					{
						melhorJogada();
						displayOptions();
						break;
					}
					case 3: 
					{
						jogadaLegal();
						displayOptions();
						break;
					}
				}
				break;
			case 4:
			{
				Quit = true;
				break;
			}



		}
	} while (!Quit);
	

	

	
}


Also, the function bestPlay() is the function that tells me the best possible play given a certain situation (is working alright and I don't think the problem comes from there, but I could be wrong). displayOptions just prints the menu without the welcoming text to the console. As you can see I also only added the cin.clear(); and the cin.ignore(numeric_limits<streamsize>::max(), '\n'); to the first case since it is the only one I'm testing. If I can make it work there I think the others will work too (after I add to each case whatever's needed to make it work of course). Thank you in advance.
I managed to make it work! I deleted the last cin >> menuChoice since it wasn't supposed to be there anyway in the first place and placed the cin.clear() before the cin >> menuChoice.
Anyway, if you are interested, my menu system is like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void SistemaMain(int Sistema)
{
	int Escolha;

	// Configurações:
	if (Sistema == 1) {Function(0); }
	if (Sistema == 10) { Function(1); }
	if (Sistema == 11) { Function(2); }
	if (Sistema == 2) { Function0(0); }
	if (Sistema == 21) { Function0(1); }
	if (Sistema == 23) { Function0(2); }
	if (Sistema == 3) { Function1(0); }
	if (Sistema == 31) { Function1(1); }
	if (Sistema == 32) { Function1(2); }
        while (Sistema == 81)
	{
		cout << "Digite 1 para sair!\nDigite 2 para continuar!\n";
		cin >> Escolha;
		if (Escolha == 1) { Sistema = 7777; } // Or any number you wouldn't use
	}
}
Last edited on
Oh I never thought of doing things that way! My program works now but thanks anyway!
Topic archived. No new replies allowed.