### Nested if, Need Help!

Guys I want to make a program that tells whether your are adult, teenager, kid or old. my table is like

kid 1-12
Teen 13-19
adult 20-40
old 40-~~~

Every thing is fine except arrangement of conditions.
Also I need an explanation with your correction for better understanding :D

Code is

int main()
{
int age;

cout<<"enter age";
cin>>age;

if(age<=12) //if(condition 1)
{

if(age>=13 && age<=19) //if(condition 2)

{
cout<<"you are Teenager"; //statement 2
}

else
{
cout<<"You are a kid"; //statement 1
}

}

else
{

if(age>=20 && age<=40) //if(condition 3)
{
cout<<"You are Adult"; //statement 3
}

else
{
cout<<"You are old"; //statement 4
}

}

return 0;
}
Put code within code brackets - [.code] [./code] (without periods).

Your if/else statements are wacky. The first one checks if age is less than or equal to 12. Meaning the nested one that checks for age >= 13 is already going to be wrong, because we know that age is 12 or less.

 ``1234567891011121314151617181920212223242526272829303132`` ``````int main() { int age; cout << "enter age"; cin >> age; if (age <= 12) //if(condition 1) Are they A Kid? { cout << "You are a kid"; //statement 1 } else if (age >= 13 && age <= 19) //if(condition 2) If not a kid, Are they A teenager? { cout << "you are Teenager"; //statement 2 } else if (age >= 20 && age <= 40) //if(condition 3) If not A teen, Are they an Adult? { cout << "You are Adult"; //statement 3 } else { cout << "You are old"; //statement 4 Are they Just Really Old? } return 0; }``````

And ideally your first if statement would check if the person entered a positive integer.
Last edited on
Similar problems will result in similar solutions. Compare yours and this one: http://www.cplusplus.com/forum/beginner/253426/#msg1114432
A small amendment to the code of zapshe. The first condition in else if blocks is unnecessary. This is due to the fact that ages limits come one after the other with no gaps
 ``12345678910111213141516`` ``````if (age <= 12) //if(condition 1) Are they A Kid? { ... } else if (age <= 19) //if(condition 2) If not a kid, Are they A teenager? { ... } else if (age <= 40) //if(condition 3) If not A teen, Are they an Adult? { .. } else { ... }``````

We can get the same result if turn the if else ladder up down

 ``12345678910111213141516`` ``````if (age > 40) { ... } else if (age > 19) { ... } else if (age > 12) { .. } else { ... }``````

My point is if you already checked that age is not lower or equal than 12 there is no need to check that is greater or equal than 13. It is for sure!
Last edited on
 A small amendment

If it's just about small amendments consider this one:
 ``123456789101112131415161718192021`` ``````/* it is what it is */ #include #include using namespace std; int main() { int age; string msg("a kid"); // no condition (all are created equal) cout << "Enter age: "; cin >> age; if (age > 12) msg = "teenager"; if (age > 19) msg = "adult"; if (age > 40) msg = "old"; cout << "You are " << msg << '.'; // return 0; }``````

Method KISS -- https://en.wikipedia.org/wiki/KISS_principle
 ``12345678910111213141516`` ``````#include #include using namespace std; int main() { int ulimit[] = { 12, 19, 39 }; string designator[] = { "Kid", "Teenager", "Adult", "Experienced" }; int age; cout << "Enter age: "; cin >> age; int pos = 0; while ( pos < 3 && age > ulimit[pos] ) pos++; cout << designator[pos] << '\n'; }``````
Last edited on
Thank you very much guys. I wanted to make it in nested if. My code was right but the problem was with conditions. And I've figured it out. But anyways Thank you all a lot cause I've also learned from your codes, specially @zapshe , I am a beginner and your else if conditions are easy to understand for me.

Here's my corrected code

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` ``````#include using namespace std; int main() { int age; cout<<"enter age"; cin>> age; if(age>=13 && age<=50) { if(age>=13 && age<=19) { cout<<"you are a teenager"; } else { cout<<"you are an adult"; } } else { if(age>=0 && age<=12) { cout<<"you are a kid"; } else { cout<<"You are old"; } } return 0; }``````
Last edited on
Registered users can post here. Sign in or register to post.