d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > New To This > Switch Statement Syntax
12Next
Add Reply New Topic New Poll
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Jun 28 2015 04:10pm
In the Happy Valley School System, children are classified by age as follows:

less than 2, ineligible
2, toddler
3-5, early childhood
6-7, young reader
8-10, elementary
11 and 12, middle
13, impossible
14-16, high school
17-18, scholar
greater than 18, ineligible
Given an int variable age, write a switch statement that prints out the appropriate label from the above list based on age.

Code
switch (age)
{
case age<2:
cout<< "ineligible";
break;
case age==2:
cout<< "toddler";
break;
case age>=3 && age <=5 :
cout << "early childhood";
break;
case age >=6 && age <=7 :
cout << "young reader";
break;
case age >=8 && age <=10 :
cout << "elementary";
break;
case age >=11 && age <= 12 :
cout << "middle";
break;
case age == 13 :
cout << "impossible";
break;
case age >= 14 && age <= 16 :
cout << "high school";
break;
case age >=17 && age <=18 :
cout << "scholar";
break;
default : cout << "ineligible";

}
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Jun 28 2015 04:23pm
Code
9 switch (age)
10 {
11 case 1:
12 cout<< "ineligible";
13 break;
14 case 2:
15 cout<< "toddler";
16 break;
17 case 3||4||5 :
18 cout << "early childhood";
19 break;
20 case 6||7 :
21 cout << "young reader";
22 break;
23 case 8||9||10 :
24 cout << "elementary";
25 break;
26 case 11||12 :
27 cout << "middle";
28 break;
29 case 13 :
30 cout << "impossible";
31 break;
32 case 14||15||16 :
33 cout << "high school";
34 break;
35 case 17||18 :
36 cout << "scholar";
37 break;
38 default : cout << "ineligible";
39
40 }


that doesnt work either
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jun 28 2015 04:33pm
case needs to be constant. not a boolean evaluated at runtime.

http://en.cppreference.com/w/cpp/language/switch

This post was edited by carteblanche on Jun 28 2015 04:34pm
Member
Posts: 5,627
Joined: Apr 13 2006
Gold: 390.00
Jun 28 2015 05:33pm
What you are looking to do is something similar to the following:

Code
switch(num)
{
case 1:
dosomething();
break;
case 2:
case 3:
case 4:
dosomethingelse();
break;
}


2,3,4 will all call dosomethinmgelse();

This post was edited by SilverMice on Jun 28 2015 05:34pm
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Jun 28 2015 07:32pm
so i got it to function anyway
is this really the best answer? theres no way to range these ? i mean what if there was a group that covered age 18-55, i sure wouldn't want to type out all those cases.
Code
switch (age)
{
case 2:
cout<< "toddler";
break;
case 3:
case 4:
case 5:
cout << "early childhood";
break;
case 6:
case 7:
cout << "young reader";
break;
case 8:
case 9:
case 10:
cout << "elementary";
break;
case 11:
case 12:
cout << "middle";
break;
case 13:
cout << "impossible";
break;
case 14:
case 15:
case 16:
cout << "high school";
break;
case 17:
case 18:
cout << "scholar";
break;
default : cout << "ineligible";
}


ty btw silver spot on advice, easy to understand, easy to implement.

This post was edited by Ideophobe on Jun 28 2015 07:44pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jun 28 2015 07:53pm
welcome to my home flaps
Member
Posts: 13,578
Joined: Jul 27 2010
Gold: 2,285.00
Jun 28 2015 08:03pm
Are you missing something in each of those cout statements that would insert the newline and flush the buffer?

cout << message << endl;
Member
Posts: 5,627
Joined: Apr 13 2006
Gold: 390.00
Jun 30 2015 06:38pm
Quote (Ideophobe @ Jun 28 2015 07:32pm)
so i got it to function anyway
is this really the best answer? theres no way to range these ? i mean what if there was a group that covered age 18-55, i sure wouldn't want to type out all those cases.
Code
switch (age)
{
case 2:
cout<< "toddler";
break;
case 3:
case 4:
case 5:
cout << "early childhood";
break;
case 6:
case 7:
cout << "young reader";
break;
case 8:
case 9:
case 10:
cout << "elementary";
break;
case 11:
case 12:
cout << "middle";
break;
case 13:
cout << "impossible";
break;
case 14:
case 15:
case 16:
cout << "high school";
break;
case 17:
case 18:
cout << "scholar";
break;
default : cout << "ineligible";
}


ty btw silver spot on advice, easy to understand, easy to implement.


Np,

unfortunately as to your question the answer is no not with a switch.

If you were able to use if else statements you could use ranges similar to the following:

Code
if(num > 0 && num <= 99)
{
dosomething();
}
else if(num >= 100 && num <= 199)
{
dosomethingagain();
}
else if (num >= 200 && num <= 299)
{
dosomethingelse();
}
else
{
dosomethingcompletelydifferent();
}


/e for syntax.

This post was edited by SilverMice on Jun 30 2015 06:39pm
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Jul 1 2015 09:32pm
Quote (Eep @ Jun 28 2015 07:53pm)
welcome to my home flaps


ye took up a hobby, hope ya dont mind me swingin by if i got a quick question every now and again.
took a couple semesters of oracle to build a database for the company, now i wanna integrate that with a gui sales suite figured c++ was a good place to start

This post was edited by Ideophobe on Jul 1 2015 09:44pm
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Jul 1 2015 09:34pm
Quote (SilverMice @ Jun 30 2015 06:38pm)
Np,

unfortunately as to your question the answer is no not with a switch.

If you were able to use if else statements you could use ranges similar to the following:

Code
if(num > 0 && num <= 99)
{
dosomething();
}
else if(num >= 100 && num <= 199)
{
dosomethingagain();
}
else if (num >= 200 && num <= 299)
{
dosomethingelse();
}
else
{
dosomethingcompletelydifferent();
}


/e for syntax.


thanks again man but ya the question required a switch statement. there were a bunch that required if/else and ? : in the chapter as well but out of like 150 practice problems that one switch question i just could not make work.
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll