#include <iostream>
usingnamespace std;
int main()
{
int answer;
cin>>answer;
if(cin.fail())
{
do
{
cout<< "nope\n";
cin>>answer;
}while(answer==5);
}
else
cout<<answer;
}
The program should accept some input and store it into the variable 'answer'. If the input fails (i.e. it's not an integer such as the letter 'f') then it executes the do while loop.
The problem is that when you enter in 'f', it displays "nope" but doesn't ask for a new input.
I am assuming that if the user enters a 5, you want it to exit the do-while? The problem is in the conditional on your do-while. You have it set to "loop this if answer is equal to 5". I believe you want to change it to not equals? Anyway, if that is true, there is one other thing you need to do, you need to flush the input stream. The easiest way I can think of, I added to the code below. It just consists of cin.clear() and cin.get().