mardi 8 novembre 2016

about asking the user to input again when invalid input occurs [c++] [duplicate]

This question already has an answer here:

//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.

Aucun commentaire:

Enregistrer un commentaire