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.

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

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

Sorry
Wanna know if I can use them after "case"

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

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.

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;
}

OMG!!
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, networking, learning, and sharing knowledge.