Switch Program

closed account (LE3bqMoL)
If I write a partial code to read in an integer from the keyboard (the integer should be 1, 2, 3, or 4) and display to the monitor Freshman, Sophomore, Junior, or Senior respectively. If not one of these integers, display an appropriate message which I made unknow. I need to use a switch statement is this correct?


int high_school;

cout <<"Enter a number";
cin >> high_school;
high_school/=4;

switch(high_school)
{
case '1': cout <<"Freshman";
break;
case '2': cout <<"Sophomore";
break;
case '3': cout <<"Junior";
break;
case '4': cout <<"Senior";
break;
default: cout <<"Unknow";
}
You certainly can use a switch statement for this.
closed account (LE3bqMoL)
I know I can use a switch statement...I am not asking if I can I am asking am I on the right track, am I right, and if not can someone tell me what I am doing wrong?? THANK YOU
You forgot something.

When you are using the switch conditional with an integer you don't include the single quotes around the number.

All you have to do is:
1
2
3
4
5
6
7
8
case 1:
...
break;

case 2:
...
break;
...
closed account (LE3bqMoL)
Thank You...Is there anything else I should do or I forgot? I just want to make sure I think I am correct.

int high_school;

cout <<"Enter a number";
cin >> high_school;
high_school/=4;

switch(high_school)
{
case 1: cout <<"Freshman";
break;
case 2: cout <<"Sophomore";
break;
case 3: cout <<"Junior";
break;
case 4: cout <<"Senior";
break;
default: cout <<"Unknow";
}
It looks good except in the default case "Unknown" is spelled wrong. ;)
closed account (LE3bqMoL)
Thank You so much I will fix "Unknown"!!!
Last edited on
Topic archived. No new replies allowed.