Nested Switch Statements

The assignment is to output the months of the year depending on the first, second, and third character from the user. Everything seems to run fine except for case 'M' or 'm". Because the second letter is already going to be an 'A', the assignment asks that we read in the next two letters from the user. Such as,
'ar' for March or 'ay' for May. So when I enter the next to letters, either 'ar' or 'ay', it outputs the default message "unknown month".

This is the code 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <iomanip>

using namespace std;

/*********************************************************

 *********************************************************/
int main()
{
    char answer, anest, jnest, jujunest, manest1, manest2;


    cout << "Please enter the first letter of a month: " << endl;
    cin >> answer;
    cout << endl;
    
switch (answer) { // outter nested switch
    case 'F':
    case 'f':
        cout << "The month is February" << endl;
        break;

    case 'S':
    case 's':
        cout << "The month is September" << endl;
        break;
    
    case 'O':
    case 'o':
        cout << "The month is October" << endl;
        break;
    
    case 'N':
    case 'n':
        cout << "The month is November" << endl;
        break;
      
    case 'D':
    case 'd':
        cout << "The month is December" << endl;
        break;  
      
    case 'A':
    case 'a':
        cout << "Please enter the second letter of the month: " << endl;
        cin  >> anest;
            
        switch (anest) {  // April and August inner nest
            case 'P':
            case 'p':
                cout << "The month is April" << endl;
            break;
                   
            case 'U':
            case 'u':
                cout << "The month is August" << endl;
            break;
            
            default:
                cout << "Unknown Month" << endl;        
          }             // end of April August inner nest
          break;
    
    case 'J':
    case 'j':
        cout << "Please enter the second letter of the month: " << endl;
        cin  >> jnest;
      
      switch (jnest) {  // January June July inner nest
      
          case 'A':
          case 'a':
              cout << "The month is January" << endl;
              break;        
          case 'U':
          case 'u':
              cout << "Please enter the third letter of the month: " << endl;
              cin  >> jujunest;
            
              switch (jujunest) {  // July June double inner nest
      
                case 'L':
                case 'l':
                    cout << "The month is July" << endl;
                    break;
                          
                case 'N':
                case 'n':
                    cout << "The month is June" << endl;
                    break;
                
                default: cout << "Unknown Month" << endl;                        
                }    // end of July June double inner nest  
                break;         
                
      }    // end of January June July inner nest      
      break;
          
    case 'M':
    case 'm':
        cout << "Please enter the next two letters of the month: " << endl;
        cin  >> manest1, manest2;
      
        switch (manest1) { // March May inner nest
              
            case 'A': 
            case 'a': 
                 
                switch (manest2) { // March May double inner nest
                    case 'R':
                    case 'r':           
                    cout << "The month is March" << endl;
                    break;    
            
                    case 'Y':
                    case 'y':
                    cout << "The month is May" << endl;
                    break;
                    
                    default: 
                    cout << "Unknown Month" << endl;
                    ) 
            } // end of March May double inner nest
            break;
           
           default: 
                    cout << "Unknown Month" << endl;  
      }  // end of March May inner nest
      break;                    
    
    default: cout << "Unknown Month" << endl;
            
    }  // end of outer nest
    
      
    cout << endl << endl;
   
    system("PAUSE");

    return 0;
}
Last edited on
how about eliminating the case 'A'/'a' and just ask to type the third letter them, the appearance will not quite good thought
Line 103 should be cin >> manest1 >> manest2 ; The comma operator isn't going to do what you want.
It solved !
Topic archived. No new replies allowed.