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.
{
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?