case statements

You guys were a great help with my first problem so I thought I'd post this one as well.

Its pretty obvious what I'm going for here. If I enter m then Marry will display, t for Thomas, s for Susan and anything else will display Nobody. For some reason, regardless of what I put in, Nobody displays.

I'm not asking anyone to completely do it for me (or else I'll never learn) but a hint or a point in the right direction would be great. Thank you in advance.

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 <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int m, t, s;
    
    cout << "Please enter a character (m, t, s):";
    cin >> m, t, s; 
   
    switch () {
  case 'm': 
    cout << "Marry";
    break;
  case 't':
    cout << "Thomas";
    break;
    case 's':
    cout << "Susan";
    break;
  case '':
    cout << "Nobody";
  }
     
     system("PAUSE");
    return EXIT_SUCCESS;
}

Last edited on
You #include files you don't need.

m, t, s are all ints, but you look for chars.

cin isn't doing what you think. To get input for multiple variables, you have to use the ">>" operator each time (cin>> /*var1*/>> /*var2*/>> /*var3*/;).

You don't give a variable to the switch statement (switch (/*variable*/)).

Your last case doesn't have a break.

I won't get into system (), someone else will.

You can just return 0;
Okay I revised my code to this and now regardless of what I put in, nothing displays.
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
31
32
33
34
#include <cstdlib>
#include <iostream>
#include <string>


using namespace std;

int main()
{
    int x;
    
    cout << "Please enter a character (m, t, s):";
    cin >> x; 
   
    switch (x)
    {
  case 'x == m': 
    cout << "Marry";
    break;
  case 'x == t':
    cout << "Thomas";
    break;
    case 'x == s':
    cout << "Susan";
    break;
  case 'x == a,b,c,d,e,f,g,h,i,j,k,l,n,o,p,q,r,u,v,w,x,y,z':
    cout << "Nobody";
    break;
  }
     
     system("PAUSE");
    return 0;
}

Okay, the variable that you declare has to be char, not int.

The case statements were right before, you can't do comparison in a case statement:

1
2
3
4
5
6
switch (a)
{
    case 1:
        //checks to see is a is one (the same as if (a == 1))
    break;
}
Last edited on
*facepalm*

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
#include <iostream>

using namespace std;

int main()
{
    char x;
    
    cout << "Please enter a character (m, t, s):";
    cin >> x; 
   
    switch (x) {
  case 'm': 
    cout << "Marry";
    break;
  case 't':
    cout << "Thomas";
    break;
    case 's':
    cout << "Susan";
    break;
  default:
    cout << "Nobody";
    break;
  }
     
}


Here is what has been changed from your original code:

char x;

Because x is a character, not an integer.

Also, you don't need to put case 'x == a,b,c,d,e,f,g,h,i,j,k,l,n,o,p,q,r,u,v,w,x,y,z': for every other possible value. Imagine if you were using a number, would you input every other possible number up to infinity? No, if it seems like there must be an easier way to do it, then there definitely is. By doing this:

1
2
3
  default:
    cout << "Nobody";
    break;


This will output "Nobody" for any other value that is not mentioned in the switch statement :))
Hope this helps and feel free to ask if there is something you did not understand.

EDIT: I will also not go into why you should not use system (); because you will probably come back to this forum sometime in the future, and no doubt someone will explain it to you.
Last edited on
Hope this helps and feel free to ask if there is something you did not understand.


It did help... very much. I really appreciate you guys taking the time to help me out. This is my first programming language and I know a lot of this is *facepalm* :)
Topic archived. No new replies allowed.