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.

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.

include<iostream>

using namespace std;

int main()

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

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

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

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",
    "Aged",
    "Middle-Age",
    "Thirties",
    "Young-Adulthood",
    "Adolescence",
    "School-Age",
    "Preschool-Age",
    "Childhood",
    "Infancy"
};
const int AgeRange[] = 
{
    65,
    48,
    40,
    30,
    18,
    13,
    6,
    4,
    2,
    1
};
string GetCategory(int age)
{
    string retval = "";
    int i = 0;
    for(; i < 10; i++)
        if(age >= AgeRange[i])
            break;
     //Using the following would eliminate the whole switch block
    //retval = Categories[i];
    switch(i)
    {
        case 0:
            retval = Categories[0];
            break;
        case 1:
            retval = Categories[1];
            break;
        case 2:
            retval = Categories[2];
            break;
    // and so on ...
    }
    return retval;
}

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

Edited 3 Years Ago by tinstaafl

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";
        break;
    case 2: case 3:
        cout << "infancy";
        break;
    // etc. ad nauseam.   
    }
    return 0;
}

Edited 3 Years Ago by Nutster: Added comment. Fixed one last capitalization.

This article has been dead for over six months. Start a new discussion instead.