switch statements

My assignment is:

Ask the user for a number.  Use a switch statement to process the number entered:
if the user enters 2, print "shoe"
if the user enters 4, print "door"
if the user enters 6, print "sticks"
if the user enters 8, print "straight"
if the user enters 10, print "big fat hen"
if the user enters any other number, print out "Not much into kids' rhymes, are you?"

Here's what I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 #include<iostream>
using namespace std;

int main()
{
  char number;
  
  cout << "Please enter a number" << endl;
  cin>> number;

  cout << number << endl;
  switch (number)
  { case 2: cout << "shoe";
            break;

    case 4: cout << "door";
            break;

    case 6: cout << "sticks";
            break;
    case 8: cout << "straight";
            break;
    case 10: cout << "big fat hen";
            break;
   }

    }

                                                              1,1           Top

I've never done a switch statement before so I know this isn't completley right. I'm also not sure how to have it print out the code for all the other numbers that aren't listed.
You shall substitute for example case 2: for case '2': and so on.
Can't help you with that but heres a hint:
1
2
3
case 2:
{
} //Your Missing the brackets 

@Pickle Gunner
Can't help you with that but heres a hint:
case 2:
{
} //Your Missing the brackets


Neither brackets are required.
Ignore vlad's advice to change the case statments from 2 to '2' and so on, and instead just change char number; to int number; so that you're reading an 'int'eger instead of a single 'char'acter. That should make it work for the most part.
If you want to make it do something if it doesn't match any of your cases in the switch statement, just add one called 'default'. Like this:
1
2
3
4
5
Case 10: cout << "big fat hen";
break;
default:
//do stuff
break;


Also, re: Pickle Gunner, { / } brackets are unnecessary inside of switch statement cases.
Last edited on
@computermajor12


Also you have to include label default: in the switch statement.
The switch statement looks more or less ok...

But you should be using an int for you number. As it stands, entering 2 witll be tweated as the char '2', which has an Ascii code of 50. So it won't display shoe! (And changing 2 to '2', etc won't help with case 10 in the switch statement as there is no char '10'!)

Then:

- to handle the other numbers, use a default. I have been taught to always provide a default. I always add it to the end of the switch statement, I don't give it a break

- while you can get away with omitting the return from main (it's a special case), I think it is good form to include it.

- you should keeps you brackets tidy. it can help to find mis-matched brackets.

Andy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;

int main()
{
  int number = 0;

  cout << "Please enter a number" << endl;
  cin >> number; // note this does not guard against random i/p

  cout << number << endl;
  switch (number)
  {
    case 2: cout << "shoe";
            break;
    case 4: cout << "door";
            break;
    case 6: cout << "sticks";
            break;
    case 8: cout << "straight";
            break;
    case 10: cout << "big fat hen";
            break;
    default: cout << "unknown number";
  }

  cout << endl;

  return 0;
}
Last edited on
Topic archived. No new replies allowed.