2016-11-14 110 views
0

我有一個問題與我的開關,其中一個值完成其操作後,我沒有給出一個選項來重做菜單,但是當我選擇b作爲我的值的菜單我給了選項來重做菜單並再次播放。 我找不到原因,因爲測試是在開關狀態之外完成的。C + + 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; 
} 
+1

您是否嘗試過使用調試器 – Danh

+1

編譯器對switch語句非常「隨意」。我總是對'允許'是什麼感到驚訝。首先,我建議你加上大括號......人類通常不會完成任務。此外,找到一個工具來縮進您的代碼,我會搜索'漂亮的打印'程序。 –

回答

3

因爲你從函數在a開關箱的最後返回:

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

我有我的開關一個問題,後一個值完成相應操作我不給出選項重做菜單,

那是因爲你有

return 0; 

在該分支中。


如果簡化代碼,將會更容易避免此類錯誤。不要在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; 
}