Beginner Function Help


I am asking for the user to enter their age and if the number is less than 0 or greater than 100 to ask them again until they type in a number between 0 and 100. Right now it is saying that "age" in the while condition is undefined but I defined right above as you can say. If anyone can help me out that would be awesome. Also I would like to make the function to where the user would have to input and integer only, for instance if they entered characters or something I want to to keep asking them until they enter a valid integer. Any advice?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void getage(void)
{
	do
	{
	cout << "How old are you, if I may ask?" << endl;
	int age;
	cin >> age;
	} while(age < 0 && age > 100);
		{
			cout << "Sorry please enter a number that makes sense!" << endl;
	}
	cout << age << "! Is that right?";
	
}
Last edited on
while(age < 0 && age > 100)

Age can't be less than 0 and greater than 100 at the same time. Maybe try this? (Logical OR instead of logical AND)

while(age < 0 || age > 100)



Edit: and the body of the do.. while statement is only between the braces right after do and before while. It looks like you would want the "Sorry.. " message to output if the while condition (bad data) is true? Right now you have that outside the do... while loop.
Last edited on
Topic archived. No new replies allowed.