开发者

While keeps repeating on letters but not on #'s

开发者 https://www.devze.com 2023-04-11 12:23 出处:网络
I have a while loop here that only takes in 1 and 2 as the number, if i insert and number that is not these my else statement will keep asking for the correct one, which works correctly. But if i inse

I have a while loop here that only takes in 1 and 2 as the number, if i insert and number that is not these my else statement will keep asking for the correct one, which works correctly. But if i insert a letter my else statement loops forever. How can i fix this?

#include <iostream>
using namespace std;

int main()
{
int myChoice;
cin >> myChoice;

while ( myChoice >= 2 ||  myChoic开发者_运维知识库e <= 1)
{
    if (myChoice == 1)
    {
        cout <<"food1";
        break;
    }
    else if (myChoice == 2)
    {
        cout <<"food2";
        break;
    }
    else
    {
        cout << " " << endl;
        cout << "Please select the proper choices" << endl;
        cout << "Try again: ";
        cin >> myChoice;
    }
}
return 0;
}


If you enter a non-number, then cin >> myChoice fails. That means that it leaves the input intact in the input buffer and when you get there again it tries to parse it and fails, and so on... You must clear the error state and ignore the non-digits. The simplest way is something like this:

cout << "Try again: ";
cin.clear(); // clear error state
cin.ignore(std::numeric_limits<streamsize>::max(), '\n'); // ignore till the end of line
cin >> myChoice;


The problem here is that the cin >> operator expects to receive an int input and receives a char input.

The istream module, of which cin is an instance, is using buffered I/O. This means that the user input is first stored in a buffer, and then read from that buffer when the user program accesses the >> operator. Ordinarily, if the >> operator succeeds in reading and parsing the user input, the read data is extracted from the buffer and the next invocation of the >> operator would continue where the last call left off. In you case, however, the >> operator attempts to parse the user input as a number and fails since it contains illegal chars which are not digits. The >> operator doesn't extract the read data from the buffer in this case and this same data is being referred to over and over again in the following calls to the >> operator.

You should empty the buffer on failure, the way ybungalobill suggested, for instance.


Your while condition is always true, then you use break to exit the loop. You could simplify things a bit like this:

#include <iostream>
using namespace std;

int main()
{
  int myChoice;
  cin >> myChoice;

  while( myChoice != 1 || myChoice != 2 ) {
    cout << endl;
    cout << "Please select the proper choices" << endl;
    cout << "Try again: ";
    cin.clear();
    cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
    cin >> myChoice;
  }

  // At this point myChoice is 1 or 2

  if (myChoice == 1)
    cout << "food1";
  else if (myChoice == 2)
      cout << "food2";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号