Is there an alternative to this switch?

Apr 25, 2013 at 6:19pm
Is there an alternative to this switch statement? The switch statement works but I don't like the way it looks in the code. It just seems like there should be a shortcut that involves an if loop and an array.

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
  {
	char dayNameCh[2]; // an array to hold the first 2 chars of the day name entered by the user
	string ignore; // a variable to handle the rest of the day name that is not used

	cout << "Enter the day you would like to set:   ";

			for (int index = 0; index < 2; index++) // get the first 2 chars in the users input
				{                                   // and assigns them to the array
				cin >> dayNameCh[index];
				}
			 getline (cin,ignore); // ignore the rest of the input
	
	switch (dayNameCh[0]) //reads the array and determins what weekday to set day to. 
		{
		case 'S':
		case 's':
			if (dayNameCh[1]== 'u' || dayNameCh[1] == 'U')
			{
				day = "Sunday";
				dayNumber = 1;
			}
			else 
			{
				day = "Saturday";
				dayNumber = 7;
			}
			break;

		case 'T':
		case 't':
			if (dayNameCh[1]== 'u' || dayNameCh[1] == 'U')
			{
				day = "Tuesday";
				dayNumber = 3;
			}
			else 
			{
				day = "Thursday";
				dayNumber = 5;
			}
			break;

		case 'M':
		case 'm':
			{
				day = "Monday";
				dayNumber = 2;
			}
			break;

		case 'W':
		case 'w':
			{
				day = "Wedensday";
				dayNumber = 4;
			}
			break;

		case 'F':
		case 'f':
			{
				day = "Friday";
				dayNumber = 6;
			}
				break;
		default:
			cout << "Your entry could not be mached to a weekday. Please try again." << endl; // ERROR handling
			promptUser();
		}
}


possibly something using an array of strings with the weekdays in it?

1
2
string dayName[] = {"Sunday", "Monday", "Tuesday", "Wedensday", "Thursday", "Friday", "Saturday"};
Apr 25, 2013 at 6:26pm
In general switch statements are shorter ans easier to read and array would be useless that's an extra line since each day only used once
Apr 25, 2013 at 6:36pm
http://www.cplusplus.com/reference/cctype/tolower/

You could use std::map, where key is two lowercase letters and value is a pair of dayName and dayNumber.
Apr 25, 2013 at 6:51pm
Well if that's what he is doing.. you could do something like.
1
2
3
4
5
6
7
    string day, array[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    std::cout << "Please enter a day: " << std::flush;
    getline(cin, day);
    day[0] = toupper(day[0]);
    for(unsigned int i = 1; i<day.size(); i++) day[i] = tolower(day[i]);
    for(unsigned int i = 0; i<7; i++) if(day == array[i])
        std::cout << "The entered day was: " << day << " which is day " << i+1 << " of the week." << std::endl;

Apr 25, 2013 at 7:27pm
Oh. Very nice. That is what I was looking for. I just couldn't visualize it without seeing it. Thank you all so much :]
Topic archived. No new replies allowed.