0
//User input
cout << "Please enter the property value: ";
while (true)
{
    cin >> property_value;
    if (cin.fail())
    {
        cout << "Please enter the valid data." << endl;
        cout << "Please enter the property value again:" << endl;
    }
    else
    {
        break;
    }
}

I want to ask the user to reinput again when the user input the invalid data, e.g. some letters. However when I try to run it, it keeps showing "Please enter the valid data." and "Please enter the property value again:", it enters an infinite loop. I already check some post also about this problem, but my code is similar to them. Please help. P.S. sorry for my bad english.

  • What post did you check? – Scott Hunter Nov 08 '16 at 16:26
  • http://stackoverflow.com/questions/39193327/how-to-ask-for-input-again-c and etc. – Tom Chan Nov 08 '16 at 16:28
  • Insert `cin.clear()` at the end of the first `if`. – David G Nov 08 '16 at 16:30
  • it still keeps showing those two cout. =[ – Tom Chan Nov 08 '16 at 16:32
  • //User input cout << "Please enter the property value: "; while (true) { cin >> property_value; if (cin.fail()) { cin.clear(); cout << "Please enter the valid data." << endl; cout << "Please enter the property value again:" << endl; } else { break; } } – Tom Chan Nov 08 '16 at 16:32
  • How about cin.ignore() ? I saw some post talking about this. sorry that i am new to c++, what mean by cin.ignore() ? isnt cin.clear already clear the data that inputed? – Tom Chan Nov 08 '16 at 16:36

0 Answers0