I have a nasty issue with 'cin' in loops. The first time through it works fine (I want to accept only an int for a function call in my program, and ignore chars that would mess it up) but subsequent 'cin' calls are ignored and the program loops indefinitely. Here's a short program to demonstrate:
Code:
#include <stdlib.h>
#include <iostream>
using namespace std;
int main()
{
int input = 0;
while(true)
{
cout<<"Write something then press enter: \n";
cin>> input; //<- Doesn't get called after the first time
cin.ignore();
if(input == 5)
break;
else
cout<<"Invalid input.\n";
system("PAUSE");
}
system("PAUSE");
return EXIT_SUCCESS;
}
I've put in an extra system("PAUSE") call to stop it looping forever.
Any suggestions on how to fix it?