[Solved] Loop repeats indefinitely after break (c++)


It might be because you entered a wrong data type into ‘cin’. This makes cin fail and it repeats the last acceptable value in the command prompt. Nothing to do with the loop. If you try it outside of the loop you’ll see the same thing happen.

It’s good practice to do the following:

while (!(cin >> input))
{
    cin.clear();
    cin.ignore(256, '\n');
    cout << "Bad input. Try again: ";
}

so if cin fails – it will clear the fail status, ignore the last input and ask the user to enter his input again.

Hope this helps

solved Loop repeats indefinitely after break (c++)