#include <iostream>
#include <limits>
using namespace std;
int main() {
int num1;
cout<<"\nEnter first number:\n";
cin>>num1;
while(!cin.good()) { //this means while input is not valid, do this.
// if input is valid, this code will not execute
cin.clear(); //clear the error state
//ignore all characters left in the buffer
cin.ignore(numeric_limits<streamsize>::max(),'\n');
//let the user know it was wrong
cout<<"That is not a valid number. Try again.\n";
//let them enter the number again if it was not good, and it will check again
cin>>num1;
//if input is good, it will exit the loop and go on to the next step
}
cout<<num1<<endl;
return 0;
}
'c·c++ > c++ 프로그래밍' 카테고리의 다른 글
문자열 경우의 수 c++버전 (0) | 2011.12.28 |
---|---|
gcc 로 컴파일하기 (0) | 2011.12.16 |
cin 버퍼에 남은 글자수 세기 (0) | 2011.12.03 |
c++ 윈도우에서 키보드를 모니터하기 kbhit() (0) | 2011.12.03 |
c++ cin>> 대신 사용하는 안전한 입력방식 (0) | 2011.12.03 |