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!
void main()
{
unsignedint 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.