简体   繁体   English

C ++ switch语句错误

[英]C++ switch statement error

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. 我的开关出现问题,在a值完成其操作后,我没有重做菜单的选项,但是当我选择b作为菜单的值时,我有重做菜单并播放的选项再次。 i cannot find a reason for why because the test is done outside of the switch statment. 我找不到原因,因为测试是在switch语句之外进行的。

#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;
}

Because you return from the function at the end of a switch case: 因为您是a切换条件结束时从函数返回的,所以:

cout << "Thank you for playing!\n";
return 0;

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. 不要在case语句下放置大量代码,而要创建帮助器函数并调用这些函数。

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM