Mail Archives: djgpp/2001/07/24/13:23:43
Two questions:
1.) I'm using the function below in my program to get input from the
user. Everything compiles ok, but if the user enters a char instead of
int, or any other invalid input, the program goes into an infinite loop.
Presumably, this is because cin.sync() is not flushing the standard
input buffer.
2.) Also, if the user attempts to exit the program by clicking on the
close window button on the upper righthand corner of the MS DOS window
(instead of entering data), the program crashes with the blue-screen of
death.
Any insight into what I'm doing wrong would be greatly appreciated.
// function to retrieve the input from the user
void getnumber(char sentence[], int* input){
cout<<sentence;
while(!(cin>>*input)){
cin.sync();
cin.clear();
cerr<<"Invalid Input"<<endl
<<"Try again. "<<sentence;
}
cin.sync();
}
- Raw text -