Question about swutch statement

//My swutch statement is not working correctly ..when I key in May iit is still giving me March and the same thing happens with January and june, Can somone tell me hwat I am doing wrong

#include<iostream>
#include<string>
using namespace std;

int main()
{

char letter;
char letter1;
char letter2;
char letter3;
char month;


cout << endl;


cout << "Please enter a letter to identify a month " ;

cin >> letter;

cout << endl;

switch (letter) {
case 'J':
case 'j':
cout << "Enter a second letter " ;
cin >> letter1;
cout <<endl;
cout <<"You chose January" << endl;
break;

case 'F':
case 'f':
cout<<"You chose February" << endl;
break;

case 'M':
case 'm':

cout << "Please choose a second and third letter ";
cin >> letter1>>letter2;
cout <<endl;
cout << "You chose March" << endl;
break;

case 'A':
case 'a':
cout <<" Enter and second letter ";
cin >> letter1;
cout <<endl;
cout << "You chose April";
break;

case 'Y':
case 'y':
cout << "Please choose a second and third letter ";
cin >> letter1>>letter2;
cout << "You chose May";
break;

case 'U':
case 'u':
cout << "Please choose an= second and third letter ";
cin >> letter1>>letter2;
cout <<" You chose June";
break;






default:
cout<< "month does not exist" <<endl;
}
cout<<endl<<endl;
system ("PAUSE");
return 0;
}
I think what you are trying to do is type in an M for May, and J for June. This will not work because for May to be outputted u should type in y || Y and for June u should type u || U. That's the way u set the cases:

case 'U':
cout<<"You chose June";
..........
.........
case 'Y':
cout<<"You chose May";
It may be simpler to use enum.
e.g. enum Month {January=1,February,Marcch, etc......};
Then:
switch(Month)
Then:
case 1.//deals with January etc.
Oh, God. That's awful. That's the most horrible thing I've seen in quite a while.

Wouldn't it be easier to do this?
1
2
3
4
5
6
std::string month;
std::cin >>month;
std::transform(month.begin(),month.end(),tolower);
if (month=="jan"){
}else if (month=="feb"){
//... 
Once you enter a branch of the switch, it does not repeat the test. So no matter how many extra letters you input, the switch is only branching on 'letter' -- not 'letter1' or 'letter2'. Hence, that case 'U' won't work. You have to add additional logic to switch among additional letters.

So, to separate things, you need a nested switch:
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
35
36
37
38
39
40
cin >> letter;
switch (letter)
  {
  case 'J':  // January, June, or July
  case 'j':
    cout << "Please enter a second letter: " << flush;
    cin >> letter1;
    switch (letter1)
      {
      case 'A':
      case 'a':
        cout << "January\n";
        break;

      case 'U':
      case 'u':
        cout << "Please enter a third letter: " << flush;
        cin >> letter2;
        switch (letter2)
          {
          case 'N':
          case 'n':
            cout << "June\n";
            break;
          case 'L':
          case 'l':
            cout << "July\n";
            break;
          default:
            cout << "Fooey. Invalid month\n";
          }
        break;

      default:
        cout << "Fooey. Invalid month\n";
      }
    break;

  case ...
  }


However, it would be a great deal simpler to simply ask the user to input a month name and select on the string:

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
35
36
37
38
39
40
41
42
43
44
45
46
#include <algorithm>
#include <cctype>
#include <functional>
#include <iostream>
#include <string>
using namespace std;

const char* months[] =
  { 
  "jan", "feb", "mar", "apr", "may", "jun",
  "jul", "aug", "sep", "oct", "nov", "dec"
  };

int main()
  {
  string user_input;
  cout << "Please type the name of your favorite month> " << flush;
  getline( cin, user_input );

  // We're only interested in the first three characters
  user_input += "---";  // (but make sure there actually are three...) 
  user_input.erase( 3 );

  // Convert user's input to lowercase
  transform(
    user_input.begin(),
    user_input.end(),
    user_input.begin(),
    ptr_fun <int, int> ( tolower )
    );

  // Find the index of the matching month
  int month = find(
                months,
                months +12,
                user_input
                )
            - months;

  if (month < 12)
    cout << "The month number is " << (month +1) << ".\n";
  else
    cout << "That was not a month name.\n";

  return 0;
  }


Hope this helps.
Topic archived. No new replies allowed.