Write a program that asks the user to input their age. The program must reject unrealistic input (age > 150 or age < 0) and ask the user to input again until the age is valid.
#include <iostream>
using namespace std;
int main()
{
bool flag = true;
int age = 0;
while (flag)
{
cout << "Enter your age: ";
cin >> age;
if (age > 0 && age < 150)
{
cout << "Age valid!" << endl;
flag = false;
}
else
{
cout << "You enter invalid age. Try again." << endl;
}
}
}
Comments
Leave a comment