私は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;
}
申し訳ありませんが、この問題は何らかの理由で愚かであり、事前に読んでいただきありがとうございます。
C++の配列はゼロベースです。そして、「たぶん、私はちょうど「シネ」と整数を使うべきだったのだろうか? - はい。 –
文字 '1'と整数1は異なるものです –
ところで、1文字の変数は1文字の配列よりも扱いが簡単です。 –