Arya
0
Q:

integer type validation c++

int x;
    std::cin >> x;
    while(std::cin.fail()) {
        std::cout << "Error" << std::endl;
        std::cin.clear();
        std::cin.ignore(256,'\n');
        std::cin >> x;
    }
1
cout << endl << "Introduce an integer: ";
p = scanf("%d", &x);       //Scanf returns a false when the value doesn't match the correct format.
getchar();

while (p == false)
{   cout << endl << "You didn't introduce an integger. Try again.";

 	cout << endl << "Introduce an integer: ";

    p = scanf("%d", &x);
  	getchar();
}
1

New to Communities?

Join the community