Hi i'm having an issue with the text in cases 3 and 4 not displaying on screen.
Once the player has selected case 3 or 4 its will display the option you have selected but not the text that goes along with the case
This is part of my text adventure.
I'm new to coding. Any tips on the switch statement would be great :)
cout <<" Select 1 - To bowl\n";
cout <<" Select 2 - Walk away\n";
int pGame;
cout <<"Select 1 or 2: ";
cin >>pGame;
switch (pGame)
{
case 1:
cout <<" Select 3 - Throw normal\n";
cout <<" Select 4 - Trick shot\n";
int throwBall;
cout <<"Select 3 or 4: ";
cin >>throwBall;
break;
switch (throwBall)
{
case 3:
cout <<"You ready yourself for the shot….\n";
cout <<"You throw the bowling ball…\n";
cout <<"The bowling ball goes straight and true down the lane,\n hitting the pins in the sweet spot that you know so well.";
cout <<"You jump in the air with ecstasy.\n";
cout <<"As you come down you look at the screen only to notice that all but one pin is standing, typical……….\n";
break;
case 4:
cout <<"You throw the ball over arm, unexpectedly the physics freak out!\n";
cout <<"As the ball lands on the floor of the lane it bounces around the screen out of control\n";
cout <<"After several minutes of watching the ball ricochet around the screen.\n";
cout <<"The bowling ball slams into the pins.\n";
cout <<"PERFECT GAMEEEEEE!!!\n";
break;
default:
cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
} break;
case 2:
cout <<"You play it safe and walk away, but with the heavy burden of knowing you could\n have made the shot and got a perfect game";
break;
default:
cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
break;
}
cout <<"\n\n\n";
cout <<"Moving on you check the desk for any sign as to what's going on" << endl;
cin.get();
cout <<"You grab the phone in an attempt to find an answer to what's happened" << endl;
cin.get();
cout <<"You listen for a tone.. " << endl;
case 4:
cout <<"You throw the ball over arm, unexpectedly the physics freak out!\n";
cout <<"As the ball lands on the floor of the lane it bounces around the screen out of control\n";
cout <<"After several minutes of watching the ball ricochet around the screen.\n";
cout <<"The bowling ball slams into the pins.\n";
cout <<"PERFECT GAMEEEEEE!!!\n";
break;
default:
cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
} break;
should be:
1 2 3 4 5 6 7 8 9 10 11
case 4:
cout <<"You throw the ball over arm, unexpectedly the physics freak out!\n";
cout <<"As the ball lands on the floor of the lane it bounces around the screen out of control\n";
cout <<"After several minutes of watching the ball ricochet around the screen.\n";
cout <<"The bowling ball slams into the pins.\n";
cout <<"PERFECT GAMEEEEEE!!!\n";
break;
default:
cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
break;
}
Think the "break" after "cin>>throwBall" is blocking the call for the second switch. If you remove that, things should work okay.
The break that Lynx876 moved into the default case is the correct stop for pGame case 1, but it never gets there because of the first break. (Also, it shouldn't be moved. No point in a "break" for the default case and that break is part of pGame's case 1 if I'm reading correctly.)
Also, fix your indentations, because it's very confusing this way!
First, I'd initialize throwBall to 0, if you haven't already. Then, instead of nesting the throwBall switch, I'd put it after the pGame switch with an if statement around it:
switch (pGame)
{
case 1:
cout <<" Select 3 - Throw normal\n";
cout <<" Select 4 - Trick shot\n";
int throwBall;
cout <<"Select 3 or 4: ";
cin >>throwBall;
break;
case 2:
cout <<"You play it safe and walk away, but with the heavy burden of knowing you could\n have made the shot and got a perfect game";
break;
default:
cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
break;
}
if (throwBall != 0)
{
switch (throwBall)
{
case 3:
...
}
And if case/switches are giving you headaches, you can always use if/else as an alternative.
Oh, and this is just a nitpick, but you might want to change the numbers on line 37 to read "3" and "4" instead of "1" and "2".
cout <<" Select 1 - To bowl\n";
cout <<" Select 2 - Walk away\n";
int pGame;
cout <<"Select 1 or 2: ";
cin >>pGame;
switch (pGame)
{
case 1:
cout <<" Select 3 - Throw normal\n";
cout <<" Select 4 - Trick shot\n";
int throwBall ;
cout <<"Select 3 or 4: ";
cin >>throwBall;
break;
case 2:
cout <<"You play it safe and walk away, but with the heavy burden of knowing you could\n have made the shot and got a perfect game";
break;
default:
cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
break;
}
int throwBall ;
switch (throwBall)
{
case 3:
cout <<"You ready yourself for the shot.\n";
cout <<"You throw the bowling ball\n";
cout <<"The bowling ball goes straight and true down the lane,\n hitting the pins in the sweet spot that you know so well.";
cout <<"You jump in the air\n with ecstasy.\n";
cout <<"As you come down you look at the screen only to notice that all but one pin is standing, typical...\n";
break;
case 4:
cout <<"You throw the ball over arm, unexpectedly the physics freak out!\n";
cout <<"As the ball lands on the floor of the lane it bounces around the screen out of control\n";
cout <<"After several minutes of watching the ball ricochet around the screen.\n";
cout <<"The bowling ball slams into the pins.\n";
cout <<"PERFECT GAMEEEEEE!!!\n";
break;
default:
cout <<"Error - Invalid input; only 3 or 4 allowed.\n";
break;
}
cout <<"\n\n\n";