do-while loop for data validation problem
I have been trying to use the do-while loop to validate an int data input and following is the code i have written.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <iostream>
using namespace std;
void main()
{
bool check = false;
int input = 0;
cout << "Please enter the age: ";
cin >> input;
do
{
if (!cin)
{
check = true;
}
cout << "Invalid data, please re-enter: ";
cin >> input;
}while (check==true);
system("PAUSE");
}
|
This infinitely executes the cout statement inside the loop without re-prompting to enter data again. Please help me to get it working. Thanks :)
Last edited on
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
do
{
if (input)
{
check = true;
}
else
{
cout << "Invalid data, please re-enter: ";
cin >> input;
}
}while (check==true);
|
This should work for you.
Last edited on
Topic archived. No new replies allowed.