0

その単純なプロジェクトは、想定どおりに終了することを除いて、すべてを実行します。

gotoステートメントを含むさまざまな方法を試しましたが、ifループを試しましたが、エラーしかありませんでした。現在のコードではエラーは発生しませんが、終了するためにどこに行けばよいかわかりません。派手である必要はありません これは私の2番目のプログラムです

// C// Guess My Number
// The classic number guessing game

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
int main()
{

srand(static_cast<unsigned int>(time(0)));  //seed random number generator

int secretNumberE = rand() % 10 + 1; 
int secretNumberM = rand() % 100 + 1;// random number between 1 and 100
int secretNumberH = rand() % 1000 + 1;
int tries = 0;
int guess;
char play;


{ 
cout << "\tWelcome to Guess My Number\n\n";
START:
cout << "Difficulty Levels\n\n";
cout << "1 - Easy\n";
cout << "2 - Normal\n";
cout << "3 - Hard\n\n";


int choice;
cout << "Choice: ";
cin >> choice;

switch (choice)
{
case 1:
cout << "You picked Easy.\n";
do
{
    cout << "Enter a guess 1-10: ";
    cin >> guess;
    ++tries;

    if (guess > secretNumberE)
    {
        cout << "Too high!\n\n";
    }
    else if (guess < secretNumberE)
    {
        cout << "Too low!\n\n";
    }
    else
    {
        cout << "\nThat's it! You got it in " << tries << " guesses!\n";
    }

} while (guess != secretNumberE);
std::cout << "Do you want to play again y/n? ";
cin >> play;
if ( play = 'y' ){
goto START;
}
else if (play = 'n') 
{
cout << " Thank you for playing. ";
return 0;
}
break;
case 2:
cout << "You picked Normal.\n";
do
{
    cout << "Enter a guess 1-100: ";
    cin >> guess;
    ++tries;

    if (guess > secretNumberM)
    {
        cout << "Too high!\n\n";
    }
    else if (guess < secretNumberM)
    {
        cout << "Too low!\n\n";
    }
    else
    {
        cout << "\nThat's it! You got it in " << tries << " guesses!\n";
    }

} while (guess != secretNumberM);
std::cout << "Do you want to play again y/n? ";
cin >> play;
if ( play = 'y' ){
goto START;
}
else if (play = 'n') 
{
cout << " Thank you for playing. ";
return 0;
}
break;
case 3:
cout << "You picked Hard.\n";
do
{
    cout << "Enter a guess 1-10: ";
    cin >> guess;
    ++tries;

    if (guess > secretNumberH)
    {
        cout << "Too high!\n\n";
    }
    else if (guess < secretNumberH)
    {
        cout << "Too low!\n\n";
    }
    else
    {
        cout << "\nThat's it! You got it in " << tries << " guesses!\n";
    }

} while (guess != secretNumberH);
std::cout << "Do you want to play again y/n? ";
cin >> play;
if ( play = 'y' ){
goto START;
}
else if (play = 'n') 
{
cout << " Thank you for playing. ";
return 0;
}
break;

default:
cout << "You made an illegal choice.\n";
goto START;
return 0;
}}}
4

3 に答える 3

2
if ( play = 'y' ){
goto START;
}
else if (play = 'n') 
{
cout << " Thank you for playing. ";
return 0;
}

if等値演算子ではなく代入演算子を使用しているため、最初の式は常に true になります。==2 つの値を比較するには、 を使用する必要があります。

于 2013-09-11T23:43:21.960 に答える