bool start(void)
{
string option;
cout<<"\nChoose one of these options:\n\n\n";
cout<<"[1] start game \n\n";
cout<<"[2] exit\n";
cin>>option;
system("cls");
if(option!="1"&&option!="2")
{
cout<<"\nInvalid choice!Please try again.\n\n";
cin.clear();
cin.ignore(1000,'\n');
start();
}
if(option=="1")
{
returntrue;//return to main function
}
if(option=="2")
{
cout<<"Thank you...";
cin.ignore(1000,'\n');
returnfalse; //return to main function
}
}
it has a problem.
if user enter a wrong choice(like "vnkjfnvfj") and then enter 1 program finished.
but if he or she inter 1(without entering wrong choice) program run correctly.
why????
because you are using recursion, the first start() executed with the input as "wefwefwefwef" results in option not equal to '1' and options not equal to '2', then instead of executing the "outer start()" you are executing the inner "start ()", giving the input of "1" returns true back to the outer start() and because your outer input for start() was "wefwefwefwef", it does not return true, but nothing as there is nothing being returned for the first if.
bool start(void)
{
string option;
do {
cout<<"\nChoose one of these options:\n\n\n";
cout<<"[1] start game \n\n";
cout<<"[2] exit\n";
cin>>option;
if (option!="1" && option!="2")
{
cout<<"\nInvalid choice!Please try again.\n\n";
}
cin.ignore(1000,'\n');
} while (option!="1" && option!="2");
if(option=="1")
{
returntrue;
}
// Option must be "2" when we get here
cout<<"Thank you...";
returnfalse;
}