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.
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
}
elseif (age >= 13 && age <= 19) //if(condition 2) If not a kid, Are they A teenager?
{
cout << "you are Teenager"; //statement 2
}
elseif (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.
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
if (age <= 12) //if(condition 1) Are they A Kid?
{
...
}
elseif (age <= 19) //if(condition 2) If not a kid, Are they A teenager?
{
...
}
elseif (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
/* it is what it is */
#include <iostream>
#include <bitset>
usingnamespace 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;
}
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.