Just a simple question. I created a function to validate numeric data coming in from cin. I read the posts and some say to use ignore(5000,'\n') or some other huge number. I found that seekg(o,ios::end); seems to work as well but I am not sure if it is going to cause any problems? I would think that it seeks to the end of the stream and effectively ignores all data in the stream. I am just not sure if this is the proper way to use seekg(). Anyway here is my simple function.
Code:int validate_input(const char*message) { int num; char ch; cout<<message; while( (ch= cin.get() )!='\n') { if((ch>='0' ) && (ch<='9')) { cin.putback(ch); cin>>num; } else { cin.clear(); //cin.ignore (50000,'\n'); cin.seekg(0,ios::end); cout<<"invalid input Please reenter: \n"; cout<<message; } } return num; }



LinkBack URL
About LinkBacks


