I have some issues with looping. I am trying to loop multiple statements. Code is below.
int MenuSelect () {
cout << endl;
cout << YELLOW << "Enter 1 for info" << endl;
cout << " " << endl;
cout << "Enter 2 to Start" << endl;
cout << " " << endl;
cout << "Enter 3 to view settings" << endl;
cout << " " << endl;
cout << "Enter 4 to quit" << endl;
int selected = 0;
string input;
cin >> input;
if (stringstream(input) >> selected) {
return selected;
}
else {
return -1;
}
return 0;
};
int Menu(int) {
int selected {};
while ((selected = MenuSelect()) == 1) {
printmessage();
}
if (selected == 3) {
cout << "Current Settings" << endl;
somefunction();
}
else if (selected == 2) {
cout << "Starting Game..... " << endl;
}
else if (selected == 4) {
cout << "Exiting....." << endl;
exit (3);
}
else {
cout << "Invalid Entry" << endl;
exit (3);
}
cout << "Below is the Deck of cards and you will get to choose 5 cards to play with. Choose wisely." << endl;
return 0;
};
So as you can see the user can see the menu, then if and else statements do the work. At the moment I have managed to loop the first menu select so if the user enters 1 it will print the message and then loop back to the menu. What I want is to also loop the 3 - current settings and if the user enters a invalid number. I've tried to accomplish it but I can't seem to do it.
if/else ifblocks into the loop body, and then either change your loop condition towhile ((selected = MenuSelect()) > 0)(works in this case since your4case just callsexit), or pull theselected = MenuSelect()out of the loop and run it again at the end of the loop body.