هذه طريقــة أخرى أفضــّلها، حيث لا تسمــح بالمدخل الخطأ وتعطـي المستخدم محاولة أخرى للإدخال الصحيــح.
#include <iostream>
using namespace std;
int main() {
int response;
while ( true )
{
cout << "Enter a number => ";
if ( cin >> response )
break;
else {
cout << "Error: Invalid input, try again ..!" << endl;
cin.clear();
cin.ignore(10000,'\n');
}
}
cout << "Valid input, " << response << endl;
return 0;
}
التعليقات