Reputation: 23
C++ rookie here, I am making this text based RPG-ish game and at the title screen, there is a option to quit. This is coded in main() as:
int main() {
bool main_menu = true;
while(main_menu == true) {
cout <<"\033[2J\033[1;1H";
cout <<GRN"Welcome to Yhawn! The text RPG straight outta 1972.\n";
cout <<"\n";
cout <<"(N)ew\n(Q)uit\n";
cout <<"> ";
std::string main_menu_select;
std::cin >>main_menu_select;
if(main_menu_select == "N" || main_menu_select == "n") {
createNewCharacter();
main_menu = false;
// error is here
if(main_menu_select == "Q" || main_menu_select == "q") {
main_menu = false;
return 0;
}
But, on the title screen, it does not take q
or Q
as an input and just loops back. This is confusing me as N
works just fine, and has the same code.
Thanks for any help you may give.
Upvotes: 1
Views: 59
Reputation: 2763
The importance of correct tabbing is apparent in this code:
if(main_menu_select == "N" || main_menu_select == "n") {
createNewCharacter();
main_menu = false;
// error is here
if(main_menu_select == "Q" || main_menu_select == "q") {
main_menu = false;
return 0;
}
Here is the code tabbed:
if(main_menu_select == "N" || main_menu_select == "n")
{
createNewCharacter();
main_menu = false;
// error is here
if(main_menu_select == "Q" || main_menu_select == "q")
{
main_menu = false;
return 0;
}
The problem becomes evident: Missing closing '}' in the N condition resulting in Q condition being inside N condition which is impossible to trigger.
Upvotes: 2
Reputation: 821
Well, that quit()
function as written does exactly nothing. The statement return 0;
in it doesn't end the whole program but just this (otherwise empty) function.
The simplest way to do what you seek is to replace the call to this function with return 0;
like this
if(main_menu_select == "Q" || main_menu_select == "q") {
//quit();
//main_menu = false;
return 0;
}
You can of course remove the lines I commented out as well as the useless quit()
function.
Upvotes: 2