IF to SWITCH??

SOLVED
Last edited on
switches are exact values, not ranges, and only work off 1 variable at a time.
so the simple way of nested switches
1
2
3
4
switch month
case 1:
   switch day
      case 1: case 2: case 3: ...case19:

is rather clunky and horrible. you can clean it up with a total re-design, but it serves no purpose. The point of a switch is either performance (it often becomes a lookup table instead of jump chains) or exploiting fall-through in a clever way (the above 20 days of the month is using fall through but its not elegant). There is no gain to be had from redoing this as a switch.

if you have to do it anyway, because school, ..
find a way to combine month and day into unique cases.
you can build an integer using a range,
eg month*100+ coefficient*(day<=value) to get something like 1042 that would tell you its month 10 and in the range 1-19 or whatever. Then you can switch off the result which is a reduced # of cases that span the ranges automatically. there is probably some simple way to cook up your cases, but you need to play with the data a little to find one. One idea may be to convert your dates from 1-365 and split that evenly with the correct starting point. I don't know if that works or not, but it should, with maybe a correction or two / round up /round down type thing.
Last edited on
You could switch on the month and then use the ?: operator on the day:
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
#include <iostream>
using namespace std;
int
main()
{
    int month, day;
    string month_name[12] =
	{ "January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December" };

    string sign = "unknown";
    
    cout << "Enter your birth month (1 - 12): ";
    cin >> month;

    cout << "Enter your birth day (1 - 31): ";
    cin >> day;

    switch (month) {
    case 1:
	sign = (day < 20 ? "Capricorn" : "Aquarius");
	break;
    case 2:
	sign = (day < 21 ? "Aquarius" : "Pices");
	break;
    case 3:
	sign = (day < 21 ? "Pices" : "Aries");
	break;
    case 4:
	sign = (day < 20 ? "Aries" : "Taurus");
	break;
    case 5:
	sign = (day < 21 ? "Taurus" : "Gemini");
	break;
    case 6:
	sign = (day < 21 ? "Gemini" : "Cancer");
	break;
    case 7:
	sign = (day < 23 ? "Cancer" : "Leo");
	break;
    case 8:
	sign = (day < 23 ? "Leo" : "Virgo");
	break;
    case 9:
	sign = (day < 23 ? "Virgo" : "Libra");
	break;
    case 10:
	sign = (day < 23 ? "Libra" : "Scorpio");
	break;
    case 11:
	sign = (day < 22 ? "Scorpio" : "Saggitarius");
	break;
    case 12:
	sign = (day < 22 ? "Saggitarius" : "Capricorn");
	break;
    }
    cout << "Zodiak sign for " << month_name[month - 1]
	 << ' ' << day << " is " << sign << ".\n";
    return 0;
}
yea I thought he wanted it all in one case. a ? or if() in the cases certainly qualifies.
@dhayden's answer is kind of what I proposed to the OP the other day - great minds think alike :+). But as per usual, another topic has been opened by the OP.

https://www.cplusplus.com/forum/beginner/280641/#msg1213413
Last edited on
Thank youuuu helped a lot. I am also learning a lot from the users here. This is a great platform for c++ beginners :>
May I ask what the string sign = "unknown"; statement does? Ive haven't encountered that yet
I reported you for deleting the OP.
It might be an idea in future that the first response to any @OP's who have less than say a 10 post history is to copy their post. Who then cares whether they disappear with their belongings.
raveneightttt wrote:
Thank youuuu helped a lot. I am also learning a lot from the users here. This is a great platform for c++ beginners :>

DO NOT delete the original post! That is rude, trollish and a waste of everyone's time and effort to help. We are NOT your unpaid tutors.
raveneightttt wrote:
May I ask what the string sign = "unknown"; statement does? Ive haven't encountered that yet

You can ASK, but why should we answer since you delete your posts when you get answers?
Its a valid default in case you screwed up your code.
if you see it print 'unknown' you know you missed something. If you did not do that, it would print the default constructed string object which is empty string and prints nothing. That could also work, but using a default you recognize is a better technique esp when you have dozens of strings in the code and don't know which one went wrong.

If you continue to delete your posts, you will not get much more help. The community here is a small one, and what you are doing is unacceptable, but you are also new with only a few posts. You have been warned.
@jonnin,

The OP created 2 threads, they have now trashed both threads by deleting the opening post. I do believe we have a pattern of a help thief. And troll.
Topic archived. No new replies allowed.