ループは、qまたはQが与えられるまで入力を受け取ります。ループは整数を期待していますが、qまたはQはループを分割して認識されません。ループでqまたはQを認識してループを解除するにはどうすればよいですか?C++ループは、与えられるまで整数を受け取ります。Q
int ReadInput; // Remembers input elements given
string ReadInputString = to_string(ReadInput); // String version of the ReadInput
do
{
bool NoErrors = true; // Make sure there were no errors while reading input
std::cout << "Enter the next element (Enter 'q' to stop): "; // Prompt user for input
cin >> ReadInput;
if (ReadInputString == quit || ReadInputString == Quit) // Enter Q or q to quit
{
break;
}
/* Validation */
if (cin.fail())
{
/* Pass by the bad input */
NoErrors = false; // Note there was a problem
cin.clear(); // Clear the bad input
cin.ignore(); // Ignore the bad input
std::cout << "Invalid number" << endl; // Error message
}
/* Add the input to the list (if there were no problems) */
if (NoErrors)
{
NumbersList.push_back(ReadInput); // Put the given number onto the end of the list
}
} while (ReadInput >= 0);
' quit'を破ると、 Quitは何だった? –
あなたのコードにもっと無意味なものもいくつかあります:: 'cin >> ReadInput;' int '値を読み込みますが、それを '' q' 'や' 'Q" 'と比較したいとします。 –
これは、ユーザーに入力を求めているものとはかなり異なっています。 –