これは基本的に私はこのループを今日では約7時間働いています。それは私がそれがどのようにすべきか振る舞いません。 cin(行はcharですが、列はintです。したがって、たとえばa1の代わりにaa1を入力すると、新しいcoordを要求する前にinvalidCoordが2回呼び出されます)。do ... whileループは私が欲しいものを表示していません
do
{
confirmShot = false;
validShot = false;
failShot = false;
cout << enterCoord;
cin >> row >> column;
if (cin.fail())
{
cin.clear();
cout << invalidCoord << endl;
failShot = false;
continue;
}
if (failShot == true)
{
row = toupper(row);
xCoord = column - 1;
yCoord = static_cast<int>(row - 'A');
if(xCoord > 9 || xCoord < 0 || yCoord > 9 || yCoord < 0)
{
cout << invalidCoord << endl;
validShot = false;
}
else
{
validShot = true;
}
}
else
{
continue;
}
currentTile = tileToSymbol(computerBoard[xCoord][yCoord]);
if (validShot == true)
{
switch(currentTile)
{
case '~':
case 'a':
case 'b':
case 's':
case 'd':
case 'p':
cout << "You have already shot at " << row << column << "." << '\n'
<< "Please pick a new coordinate!" << endl;
validShot = false;
confirmShot = false;
break;
default:
confirmShot = true;
}
}
else
{
continue;
}
}
while(!confirmShot);
出力:
Your shot: Enter the coordinates for a shot (e.g. B2) cc2
Invalid coordinates!
Pick a row value between A and J
and a column value between 1 and 10.
Enter the coordinates for a shot (e.g. B2) Enter the coordinates for a shot (e.g. B2) cc2
Invalid coordinates!
Pick a row value between A and J
and a column value between 1 and 10.
Enter the coordinates for a shot (e.g. B2) Enter the coordinates for a shot (e.g. B2) c2
Enter the coordinates for a shot (e.g. B2) c2
また、私はループはもう終了している間...やる取得する方法を見つけ出すことはできません。それは、誰かが2番目の文字として非整数を入力した場合に、フェイルステートを処理しなければならないことに気付く前に、正常終了していました。
私が話していた誰かが次のセクションにスキップする必要があると言い、プログラムをロックすると、プログラムがロックアップします。そこにコンティニューコードがあり、うまくいきました。
私は本当にこのループを正しく動作させる必要がありますが、私は自分の能力を使い果たしました。 failstateを処理してinvalidCoordを2回表示させてもいいですか(正直なところ、それはなぜ起こっているのかわかりません)、有効なエントリを受け取った後にループを終了してその日を保存しますか?
私はその行がcharであり、列がintであることを記事で述べました。 完全なコードはここにあります: https://repl.it/repls/DiscreteAltruisticNighthawk – Ernesto
私はあなたが 'cin.clear()'がしていることを誤解していると思います。エラーフラグをクリアするだけです。無効な入力はまだそこにあります。あなたがしたいと思っているようなことをするコードを書いているのは好きではありません。入力行を読み込み、それに2文字だけ含まれているかどうかを確認しますか? –
入力に '>>'を使わないでください。基本的に 'scanf'です。 – melpomene