0

ランダムな推測ゲームを実行するプログラムを作成する必要があります。ゲームは 1 から 100 までの数字で行われ、推測者は 20 回試行し、最後にもう一度プレイするかどうか尋ねられることになっています。ゲッサーが高すぎたり低すぎたりする場合は、プリントアウトのオプションも複数ある必要があります。プログラムの一部が完了しました。印刷用に他のオプションを追加する必要があることはわかっていますが、今のところ問題は、実行しようとすると成功したと表示されますが、変数 " number」が初期化されずに使用されています。明らかに初期化するために何をすべきかわかりません。(私はC++が初めてです) プログラムを更新しましたが、別の問題が発生しています。私のプログラムは実行されますが、推測が出力される数値よりも低い場合は、Too high try again および Too low try again が、数値が高すぎる場合は単に Too high try again が出力されます。また、ユーザーがもう一度プレイすることを選択したときに、試行カウンターがゲームでリセットされないことにも気付きました。最後に、勝ったとき、負けたとき、別のゲームをプレイするように求められたときにメッセージを追加する必要があり、乱数を使用してそれらの中から選択する必要があります。それを行うための最良のルートに関するアドバイスは素晴らしいでしょう

#include <iostream>
#include <iomanip> 
#include <ctime>
using namespace std;
char chr;

int main()
{
srand(time(NULL));                                           //the function that generates random numbers
int number=rand()%100+1;                                     //the range of the random numbers
int guess;                                                   //The guess is stored here
int tries=0;                                                 //The number of tries stored here
   char answer;                                                 //The answer to the question is stored here
answer='y';                  

while(answer=='y'||answer=='Y') 
{
    while (tries<=20 && answer=='y'|| answer=='Y')
    {
    cout<<"Enter a number between 1 and 100 "<<endl;          //The user is asked for a guess
    cin>>guess;                                               //The guess is stored here
    tries++;                                                 //Adding a number for every try
    if(guess==0||guess>100)                                  //If statement that produces an error message if user enters a number out of the peramiters
    {
     cout<<"This is not an option try again"<<endl;          //Error message
    }

    if(tries<20)                                            
    cout<<"Tries left: "<<(20-tries)<<endl;                   //Output to let the user know how many guess they have left

    if(number<guess);                                         //if the guess is to high
    cout<<"Too high try again"<<endl;                         //This message prints if guess it to high

    if(number>guess)                                          //if the guess is to low
    cout<<"Too low try again"<<endl;                          //This message prints if the guess is to low

    if(number==guess)                                          //If the user guesses the number
    {
     cout<<"Congratualtions!! "<<endl;                          //Message printed out if the user guesses correctly
     cout<<"You got the right number in "<<tries<<" tries"<<endl;  //Lets the user know how many guess they used
     answer = 'n';
    }
    if(tries >= 20)                                               //If the user uses all their guesses
    {
    cout << "You've run out of tries!"<<endl;                      //The message that prints when a user is out of guesses
    answer='n';
    }
    if(answer=='n')
    {
     cout<<"Would you like to play again?  Enter Y/N"<<endl;       //asking if play would like to play again
     cin>>answer;                                                  //Store users answer
     if (answer=='N'|| answer=='n')                                //if the user says no
     cout<<"Thanks for playing!"<<endl;                            //This message prints out if they choose not to play again

    else
        number=rand()%100+1;                                        //This starts the game over if they choose to play again
    }

    }
    }

cin>>chr;
    return 0;

}
4

5 に答える 5

8

[編集: コンパイラの警告を取り除くためにキャストを追加しました。]

Jim Rhodes がコメントで言ったように、問題は行にあります

srand(number>0);

srand()は乱数発生器の初期化に使用されるため、 で呼び出しても意味がありませnumber>0number。プログラムを実行するたびに異なる「シード」値が必要です。このようなシードを取得する一般的な方法は、システム時間を使用することです。

srand(static_cast<unsigned int>(time(NULL)));

#includeにアクセスするには、別のヘッダーが必要になる場合がありますtime()

于 2013-10-29T02:22:33.330 に答える
1

変数を使用する前に(常に!!)変数を初期化してください。そうしないと、これらを使用した操作内で未定義の動作(またはコパイラーエラー)が発生します。

int number = 0;
int guess = 0;
int tries = 0;
char answer = '\0';

(反対票を投じた人、コメントを疑う人向け):
もちろん、この答えはsrand(number>0);、目的の結果(つまりnumber、より適切な値で初期化する0)に適したステートメントを取得する方法を教えてくれませんが、最初に求められたコンパイル時エラーを解決しますこれは、同様のコンパイラ エラー (警告) が発生する他のシナリオに対する適切なアドバイスです。実行時に期待される適切な結果を得るためnumberにシード メソッドに渡す適切な値で初期化することsrand()は、まったく別の質問であり、そのように尋ねる必要があります。

于 2013-10-29T02:21:28.483 に答える