Answer to Question #168219 in C++ for sara

Question #168219

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.


1
Expert's answer
2021-03-04T06:01:17-0500
#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;
		}
 
	}
 
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS