### 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
 ``1234`` ``````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:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` ``````#include 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