i have something like
while(playAgain==true)
{
cout<<"new game"<<endl; //i know 'using namespace std;' is looked down upon
while(playerCard!=21)
{
*statements*
if(decision=='n')
{
break
}
...
}
}
but that break only breaks out of the first while loop when I want to break out of both of the loops
This solution is specific to your case. When the user's decision is 'n', he doesn't want to play again. So just set
playAgain
tofalse
and then break. Outer loop will be broken automatically.Use goto:
Don't cook spaghetti and extract your loops into the function:
this decomposition will also yield cleaner code since instead of hundreds of lines of ugly procedural code, you will have neat functions at different levels of abstraction.
There are basically two options to go.
Add the condition check in outer loop.
while ((playAgain==true) && (decision != '\n'))
Simply use
goto
. People are often told never to usegoto
as if it's monster. But I'm not opposed to use it to exit multiple loops. It's clean and clear in this situation.If you don't have to avoid
goto
statement, you can write