値の操作が終了した後にメニューをやり直すオプションが与えられていない私のスイッチに問題があります。ただし、メニューの値としてbを選択すると、メニューをやり直してもう一度再生するオプション。 テストがスイッチのステートメントの外側で実行されるため、理由が見つかりません。あなたはa
スイッチケースの最後に関数から戻るので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;
}
デバッガーを使用しようとしましたか? – Danh
コンパイラーはswitch文について非常に「カジュアル」です。私はいつも「許される」ものに驚いています。まず、中括弧を追加することをお勧めします。人間は一般的にはタスクに就いていません。また、あなたのコードを一貫してインデントするためのツールを見つけると、私は 'かなりのプリント'プログラムを探します。 –