2017-09-02 7 views
-1

私はC++にスーパーで新しく、友達と本を共有しています。私は単純な推測ゲームを作成しています。ユーザーは数字を想像し、コンピュータはそれを推測しようとします。私がVisual Studioでデバッグするとき、プロジェクトは推測を行い、「どうやってやったの?」と表示します。この時点で、 'feedback'変数のユーザー入力を取得する必要があります。しかし、プロンプトの後には、while文の前にすべてを繰り返すように見えます。フィードバックのchar変数に問題がありますか(ちょうど 'cin'と整数を使うべきだったのでしょうか?)、本当に明白なものがありませんか?C++宿題はループしません

//Game that attempts to guess a number from one to twenty. 

#include "stdafx.h" 
#include <iostream> 

using namespace std; 

int main() 
{ 
auto lowbound = 1; 
auto highbound = 20; 
auto guess = 10; 
auto gamecont = true; 

char feedback[1]; 


cout << " Pick a number from one to twenty in you head and I'll guess it; no cheating!" << endl << endl; 
cout << " If my guess is too low, just say (1). If too high, say (2). Say (3) if I've got it. It's (ENTER) to get going!" << endl << endl; 
cout << " Waiting on you..." << endl << " "; 

cin.get(); 

while(gamecont) 
{ 

    cout << " I'm thinking your number is " << guess << "." << endl << endl; 
    cout << " How did I do?" << endl << endl << " "; 

    cin.get(feedback, 1); 

    if (feedback[1] == 1) // The guess was too low. 
    { 
     if (guess == 10) 
     { 
      guess = 15; 
     } 
     else if (guess >= 15) 
     { 
      guess++; 
     } 
     else if (guess < 10) 
     { 
      guess++; 
     } 
    } 
    else if (feedback[1] == 2) // The guess was too high. 
    { 
     if (guess == 10) 
     { 
      guess = 5; 
     } 
     else if (guess <= 5) 
     { 
      guess--; 
     } 
     else if (guess > 10) 
     { 
      guess--; 
     } 
    } 
    else if (feedback[1] == 3) // The guess was correct. 
    { 
     gamecont = false; 
    } 


} 

return 0; 
} 

申し訳ありませんが、この問題は何らかの理由で愚かであり、事前に読んでいただきありがとうございます。

+5

C++の配列はゼロベースです。そして、「たぶん、私はちょうど「シネ」と整数を使うべきだったのだろうか? - はい。 –

+3

文字 '1'と整数1は異なるものです –

+0

ところで、1文字の変数は1文字の配列よりも扱いが簡単です。 –

答えて

1

千里の道も一歩から始まるので、あなたの最初のステップのためのいくつかの援助here's:

using namespace std; 

ドントはそれを行うに。 std::には使用する可能性のある識別子が混在しているため、問題は保証されています。

char feedback[1]; 

You'llはそう

char feedback; 

、長い1より入力する必要はありませんが、適切な以上のものです。

cout << " Pick a number from one to twenty in you head and I'll guess it; no cheating!" << endl << endl; 

std::endl

は、バッファ、二回それをする必要はありませんをフラッシュします(ほかに、それは char feedback[0];代わりの char feedback[1];されている必要がありますので、配列は0に基づいています)。単に '\n'を使用します。

std::cout << " Pick a number from one to twenty in you head and I'll guess it; no cheating!" << "\n\n"; 

you'llはfeedbackでキーの文字コードを取得します。 '1'1に等しくないので、

if (feedback == 1) 

if (feedback == '1') 

厥それをなければなりません。まだあなたのためにやるべきことが残っています。推測戦略は貧弱ですが、それはスタートになるはずです。

//Game that attempts to guess a number from one to twenty. 

#include <iostream> 

int main() 
{ 
    auto lowbound = 1; 
    auto highbound = 20; 
    auto guess = 10; 
    auto gamecont = true; 

    char feedback; 


    std::cout << " Pick a number from one to twenty in you head and I'll guess it; no cheating!" << "\n\n"; 
    std::cout << " If my guess is too low, just say (1). If too high, say (2). Say (3) if I've got it. It's (ENTER) to get going!" << "\n\n"; 
    std::cout << " Waiting on you..." << "\n\n"; 

    std::cin.get(); 

    while(gamecont) 
    { 

     std::cout << " I'm thinking your number is " << guess << "." << "\n\n"; 
     std::cout << " How did I do?" << "\n\n"; 

     std::cin.ignore(); 
     std::cin.get(feedback); 

     if (feedback == '1') // The guess was too low. 
     { 
      if (guess == 10) 
      { 
       guess = 15; 
      } 
      else if (guess >= 15) 
      { 
       guess++; 
      } 
      else if (guess < 10) 
      { 
       guess++; 
      } 
     } 
     else if (feedback == '2') // The guess was too high. 
     { 
      if (guess == 10) 
      { 
       guess = 5; 
      } 
      else if (guess <= 5) 
      { 
       guess--; 
      } 
      else if (guess > 10) 
      { 
       guess--; 
      } 
     } 
     else if (feedback == '3') // The guess was correct. 
     { 
      gamecont = false; 
     } 



    } 

    return 0; 
} 
+0

一般的に素晴らしいアドバイスです。ありがとうございます。 「私はあなたの数字がだと思っています」が繰り返され、「int intess」が10にとどまるので、これを動作させるにはしばらく時間がかかりました。他のものを実装した後(VS2017でプリコンパイルされたヘッダーをオフにした)、std :: cin.ignore();とstd :: cin.get(feedback);の場所を単に切り替えるだけで、正しく。 '\ n'は爆弾、btwです。私がまだ得られない唯一のことは、配列が0ベースであると言えば、それはとにかく別の方法で書き込まなければならないということです。私はそれらの違いを知ることができません:(。TY – SnowW1T3

+0

本当に私は正しいコードを貼り付けていませんでしたif(feedback == 1)などをif(feedback == ' 1 ')、説明は正しかったが、コードは間違っていた、ごめんなさい!配列が削除されて以来、ゼロベースのものはそれ以上重要ではありませんが、配列を持っていれば、charフィードバック[0] charフィードバック[1] ;.(Yep、申し訳ありませんが私の答えに入力します。両方のタイプミスが修正されました。 – DrSvanHay

関連する問題