Write C++ a program that reads a person's age in years and print his/her age group. See the table below.

Age Group name

1 year Childhood
2-3 years Infancy
4-5 years Preschool-Age
6–12 years School-Age
13–17 years Adolescence
18–29 years Young-Adulthood
30–39 years Thirties
40–64 years Middle-Age
56–48 years Aged

85 years Very-Old

You have to using switch statement.

Recommended Answers

All 10 Replies

We don't do your homework for you. Show some effort or an attempt and come back with a specific question.

There are a few things you could do to create it. Try complosite or chain of responsibility patterns or simple swithc case statements. If you come back with some progress I would be very happy to help you.

I think the line: 56–48 years Aged should read 65–84 years Aged


using namespace std;

int main()

Int age;
Cout <<"the person age : "<<endl;
Cin>> age;

Switch (age)
Case 1:
Cout <<"childhood";
Case 2 && 3 :
Cout <<"infancy";

\and complete the program to 80
\I just wanna know if I can use the logical and relational operator

Wanna know if I can use them after "case"

I would rather use if statements here:
if (year < 2) =childhood
elsif (year < 4) =infancy

According to this quote from an online reference

Notice that switch can only be used to compare an expression against constants. Therefore we cannot put variables as labels (for example case n: where n is a variable) or ranges (case (1..3):) because they are not valid C++ constants.

If you need to check ranges or values that are not constants, use a concatenation of if and else if statements.

You can't short circuit the case label with conditionals. However, if you must use a switch block, something like this would reduce the number of case blocks:

const string Categories[] = 
    "Very Old",
const int AgeRange[] = 
string GetCategory(int age)
    string retval = "";
    int i = 0;
    for(; i < 10; i++)
        if(age >= AgeRange[i])
     //Using the following would eliminate the whole switch block
    //retval = Categories[i];
        case 0:
            retval = Categories[0];
        case 1:
            retval = Categories[1];
        case 2:
            retval = Categories[2];
    // and so on ...
    return retval;

This way instead of 80 case blocks you only need 10.

I am going to take your code snippet and clean it up. When posting programming code, use the Code button and insert the code in the window that pops up.

C++ and related languages are very case-sensitive, so Int and int are different things. All keywords in C++, including types and control statements, like switch and case, are lower-case. To have multiple cases use the same chunk of code, list each one with its own case clause.

#include <iostream>

using namespace std;

int main(void)
    int age;
    cout << "How old is the person? ";
    cin >> age;

    switch (age)
    case 1:
        cout << "childhood";
    case 2: case 3:
        cout << "infancy";
    // etc. ad nauseam.   
    return 0;

thaankss alot guyss .. u r both hilarious. .
God bless you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.