Nested Switch Statement

This is the program I'm writing "Enter the first letter of a day: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday and print the full name of the day. If two days have the same first letter, use a nested switch statement for second letter in the name of the day." Below is my source code and I wanted to two questions. Can someone please explain how I would incorporate a nested switch statement into my program, and I also wanted to ask if someone could point if I had any other flaws in my source code. :) Thank you!

#include <iostream>
using namespace std;
int main(){
int answer=0;
char ascii;

cout<<"Enter a weekday according to its first letter: "<<endl;
cin>>ascii;

if(a==M)
answer=0;
if(a==T)
answer=1;
if(a==W)
answer=2;
if(a==T)
answer=3;
if(a==F)
answer=4;
if(a==S)
answer=5;
if(a==S)
answer=6;

switch(answer)
{
case 0:
cout<<"The weekday is Monday."<<endl;
break;

case 1:
cout<<"The weekday is Tuesday."<<endl;
break;

case 2:
cout<<"The weekday is Wednesday."<<endl;
break;

case 3:
cout<<"The weekday is Thursday."<<endl;
break;

case 4:
cout<<"The weekday is Friday."<<endl;
break;

case 5:
cout<<"The weekday is Saturday."<<endl;
break;

case 6:
cout<<"The weekday is Sunday."<<endl;
break;

default:
cout<<"Completely out of control - shouldn't be here."<<endl;

}

system("pause");
return 0;

}
Takaflaka

There are a lot of coding and logic problems with your code.
if(a==M)

a isn't defined, but you probably meant ascii.
As it stands, M is an undeclared variable. Probably you mean the character 'M'.

Apart from other similar flaws with the other choices, your answer would be wrong anyway.
1
2
3
4
if(a==S)
answer=5;
if(a==S)
answer=6;

For the same input 'S', answer would be set to 5, then changed to 6. Similar things will happen with 'T' - Tuesday or Thursday.

You have no nesting - there is no consideration of the second letter.


I don't think nested switches are very good here. The first program below uses nested ifs (and requires C++11 for the range-based for loop).

The second program (rather messily) uses nested switches. It was news to me that you could switch on anything other than an integer. However, it seems to be OK if both parts of the comparison can be converted to integers, so characters work.

I expect that other forum users can improve these two.

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
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
   string dayNames[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
   char firstLetter, secondLetter;

   cout << "Input first character: ";
   cin >> firstLetter;
   firstLetter = toupper( firstLetter );                             // easier comparison if you ensure it is upper case

   bool needTwo = ( firstLetter == 'S' || firstLetter == 'T' );      // do you need to consider the second letter as well?
   if ( needTwo )
   {
      cout << "Input second character: ";
      cin >> secondLetter;
      secondLetter = tolower( secondLetter );
   }

   string day = "";                                                  // default if nothing matches
   for ( string s : dayNames )                                       // needs C++11 for the range-based loop
   {                                                                 
      if ( s[0] == firstLetter )                                     // compare first letter
      {
         if ( needTwo )
         {
            if ( s[1] == secondLetter ) day = s;                     // compare second letter if necessary
         }
         else day = s;
      }
   }

   if ( day != "" ) cout << "Day is " << day << endl;
   else             cout << "Day not found" << endl;
}



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 <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
   char firstLetter, secondLetter;

   cout << "Input first character: ";
   cin >> firstLetter;
   firstLetter = toupper( firstLetter );                             // comparison later made in upper case

   bool needTwo = ( firstLetter == 'S' || firstLetter == 'T' );      // do you need to consider the second letter as well?
   if ( needTwo )
   {
      cout << "Input second character: ";
      cin >> secondLetter;
      secondLetter = tolower( secondLetter );                        // comparison later made in lower case
   }

   string day = "";                                                  // default if nothing matches
   switch( firstLetter )
   {
      case 'S':   switch( secondLetter )
                  {
                     case 'u':   day = "Sunday"  ;   break;
                     case 'a':   day = "Saturday";   break;
                  }
                  break;
   
      case 'T':   switch( secondLetter )
                  {
                     case 'u':   day = "Tuesday" ;   break;
                     case 'h':   day = "Thursday";   break;
                  }
                  break;
   
      case 'M':   day = "Monday"   ;   break;
      case 'W':   day = "Wednesday";   break;
      case 'F':   day = "Friday"   ;   break;
   }

   if ( day != "" ) cout << "Day is " << day << endl;
   else             cout << "Day not found" << endl;
}
Last edited on
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
#include<iostream>
using namespace std;
int main(){
	int answer = 0;
	char ascii;

	cout << "Enter a weekday according to its first letter: " << endl;
	cin >> ascii;

	if (ascii == 'M') // use single quote for char
		answer = 0;
	else if (ascii == 'T')
		answer = 1;
	else if (ascii == 'W')
		answer = 2;
	else if (ascii == 'T')
		answer = 3;
	else if (ascii == 'F')
		answer = 4;
	else if (ascii == 'S')
		answer = 5;
	/*if (a == S)
		answer = 6;*/

	switch (answer)
	{
	case 0:
		cout << "The weekday is Monday." << endl;
		break;

	case 1:
		cout << "The weekday is Tuesday." << endl;
		break;

	case 2:
		cout << "The weekday is Wednesday." << endl;
		break;

	case 3:
		cout << "The weekday is Thursday." << endl;
		break;

	case 4:
		cout << "The weekday is Friday." << endl;
		break;

	case 5:
		cout << "The weekday is Saturday." << endl;
		break;

	case 6:
		cout << "The weekday is Sunday." << endl;
		break;

	default:
		cout << "Completely out of control - shouldn't be here." << endl;

	}

	system("pause");
	return 0;

}
Topic archived. No new replies allowed.