123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
#include <iostream> #include <stdlib.h> using namespace std; int readValidMark(); int main() { readValidMark(); cout<<"\n"; system ("pause"); return 0; } int readValidMark() { int mark; cout<<"enter mark"; cin>>mark; if((mark>0 )&& (mark<100)) { cout<<"the mark is"<<mark; } else { do { cout<<"renter mark" ; cin>>mark; } while((mark<0)&&(mark>100)); } }
1234567891011121314151617181920
int read_valid_mark() { int mark; std::cout << "enter mark: " ; if( std::cin >> mark ) { if( mark >= 0 && mark <= 100 ) return mark ; else std::cout << "error: out of range\n" ; } else // non-numeric input { std::cout << "error: not a number\n" ; std::cin.clear() ; // clear error state std::cin.ignore( 1000, '\n' ) ; // throw the junk away } return read_valid_mark() ; // try again }