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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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?
{
	...
}
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* it is what it is */
#include <iostream>
#include <bitset>
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>

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
Topic archived. No new replies allowed.