Reputation: 1
i am having an issue with my switch where after the a value has finished its operation i am not given an option to redo the menu, however when i choose b as my value for the menu i am given the option to redo the menu and play again. i cannot find a reason for why because the test is done outside of the switch statment.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main (){
int seed = time(0);
srand(seed);
int num1, randomnum1, toothpicks=21, count1=1, rounds=0, p1picks;
char choice1, choice;
do{ //game menu
cout << "Welcome!\n";
cout << "Please select the game you will be playing.\n";
cout << "Choose 'a' for the RANDOM NUMBER GAME \n";
cout << "Choose 'b' for the TOOTHPICK GAME \n";
cin >> choice1;
cout << "Thank you!\n";
//game menu
switch(choice1){
case 'a': //random number game
//generate random number
randomnum1 = rand() % 100 + 1;
//intro
cout << "Welcome!\n ";
cout << "In this game, you will select a random number between 1 and 100 and see if you can guess it correctly!\n";
cout << "Please type a random number between 1 and 100. \n";
cin >> num1;
//user number validation
while(num1<1 || num1>100){
cout << "Wrong input! \n";
cout << "Input a number between 1 and 100. \n";
cin >> num1;
}
//was user correct?
while(num1!=randomnum1){
if (num1>randomnum1){
cout <<"That's not it. Try guessing a lower number.\n";
cin >> num1;
}
else if (num1<randomnum1){
cout << "Thats not it. Try guessing a higher number.\n";
cin >> num1;
}
count1++;
}
cout << "YES! Congradultions! That's the correct number!\n";
// tell them how many times it took to finish
cout << "Thank you for playing!\n";
return 0;
break;
case 'b': //toothpick game
toothpicks=21;
cout << "Welcome to the Toothpick game. \n";//intro and rules
cout << "Rules: \n";
cout << "Take turns picking the number of toothpicks. There are a total of 21. \n";
cout << "When it is your turn you choose to pick up 1, 2 or three toothpicks. \n";
cout << "The goal is to not be the player that has the last toothpick \n";
cout << "Good luck! \n\n\n";
cout << "You will go first. \n";
while(rounds<5){ //setting rounds to five
cout << "Type the number of toothpicks you will 'pick up'. 1, 2 or 3? \n";
cin >> p1picks;
while(p1picks<1 || p1picks>3){ //validation
cout << "That is not a valid input. You must type 1, 2 or 3. \n";
cin >> p1picks;
}
//menu options for user input
switch(p1picks){
case 1:
cout << "The computer choses to pick up 3 toothpicks. \n";
break;
case 2:
cout << "The computer choses to pick up 2 toothpicks. \n";
break;
case 3:
cout << "The computer choses to pick up 1 toothpicks. \n";
break;
}
rounds++;
toothpicks=toothpicks-4;
cout<<"This is the amount of toothpicks you have left: "<< toothpicks<< endl;
}
//computer winning speech
cout << "The computer has won. Sucker! \n";
break;
default:
cout << "Sorry this is not a choice! /n";
break;
}
cout << "this is inside the switch";
cout << "Would you like to continue? (Y)es or (N)? " << endl;
cin >> choice;
}while (choice == 'Y' || choice == 'y');
cout << "Thank you for playing! /n";
cout << "Goodbye!\n";
return 0;
}
Upvotes: 0
Views: 154
Reputation: 206717
i am having an issue with my switch where after the a value has finished its operation i am not given an option to redo the menu,
That's because you have
return 0;
in that branch.
It will be easier to avoid such errors if you simplify your code. Instead of putting a large number lines of code under the case
statements, create helper functions and call the functions.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int get_choice()
{
int choice;
//game menu
cout << "Welcome!\n";
cout << "Please select the game you will be playing.\n";
cout << "Choose 'a' for the RANDOM NUMBER GAME \n";
cout << "Choose 'b' for the TOOTHPICK GAME \n";
cin >> choice;
cout << "Thank you!\n";
return choice;
}
void playRandomNumberGame()
{
int randomnum1;
int num1;
int count1 = 1;
//generate random number
randomnum1 = rand() % 100 + 1;
//intro
cout << "Welcome!\n ";
cout << "In this game, you will select a random number between 1 and 100 and see if you can guess it correctly!\n";
cout << "Please type a random number between 1 and 100. \n";
cin >> num1;
//user number validation
while(num1<1 || num1>100)
{
cout << "Wrong input! \n";
cout << "Input a number between 1 and 100. \n";
cin >> num1;
}
//was user correct?
while(num1!=randomnum1)
{
if (num1>randomnum1)
{
cout <<"That's not it. Try guessing a lower number.\n";
cin >> num1;
}
else if (num1<randomnum1)
{
cout << "Thats not it. Try guessing a higher number.\n";
cin >> num1;
}
count1++;
}
cout << "YES! Congradultions! That's the correct number!\n";
// tell them how many times it took to finish
cout << "Thank you for playing!\n";
}
void playToothpickGame()
{
int toothpicks=21;
int p1picks;
int rounds = 0;
//intro and rules
cout << "Welcome to the Toothpick game. \n";
cout << "Rules: \n";
cout << "Take turns picking the number of toothpicks. There are a total of 21. \n";
cout << "When it is your turn you choose to pick up 1, 2 or three toothpicks. \n";
cout << "The goal is to not be the player that has the last toothpick \n";
cout << "Good luck! \n\n\n";
cout << "You will go first. \n";
while(rounds<5) //setting rounds to five
{
cout << "Type the number of toothpicks you will 'pick up'. 1, 2 or 3? \n";
cin >> p1picks;
//validation
while(p1picks<1 || p1picks>3)
{
cout << "That is not a valid input. You must type 1, 2 or 3. \n";
cin >> p1picks;
}
//menu options for user input
switch(p1picks)
{
case 1:
cout << "The computer choses to pick up 3 toothpicks. \n";
break;
case 2:
cout << "The computer choses to pick up 2 toothpicks. \n";
break;
case 3:
cout << "The computer choses to pick up 1 toothpicks. \n";
break;
}
rounds++;
toothpicks=toothpicks-4;
cout<<"This is the amount of toothpicks you have left: "<< toothpicks<< endl;
}
//computer winning speech
cout << "The computer has won. Sucker! \n";
}
int main ()
{
int seed = time(0);
srand(seed);
char choice1, choice;
do
{
choice1 = get_choice();
//game menu
switch(choice1)
{
case 'a': //random number game
playRandomNumberGame();
break;
case 'b': //toothpick game
playToothpickGame();
break;
default:
cout << "Sorry this is not a choice! /n";
break;
}
cout << "this is inside the switch";
cout << "Would you like to continue? (Y)es or (N)? " << endl;
cin >> choice;
}
while (choice == 'Y' || choice == 'y');
cout << "Thank you for playing! /n";
cout << "Goodbye!\n";
return 0;
}
Upvotes: 2
Reputation: 5920
Because you return from the function at the end of a
switch case:
cout << "Thank you for playing!\n";
return 0;
Upvotes: 3