123456789101112
int n; while(true){ cin >> n; if (cin.fail()|| n<=0) { cout << "Bad input. Please reenter." << endl; cin.clear(); cin.ignore(1000000,'\n'); } else break; } return n;