#include <iostream>
usingnamespace std;
int main()
{
int month; // month number (1-12), input
int day; // day of the month, input
string monthName; // month name
int daysInMonth=0; // # of days in the input month
bool invalidInput;// true if input is invalid, false otherwise
displayName();
cout<<"Enter a date in the form MM DD: ";
cin>>month>>day;
invalidInput=false; // initialize flag
switch(month)
{
case 1: monthName = "January"; break;
case 2: monthName = "February"; break;
case 3: monthName = "March"; break;
case 4: monthName = "April"; break;
case 5: monthName = "May"; break;
case 6: monthName = "June"; break;
case 7: monthName = "July"; break;
case 8: monthName = "August"; break;
case 9: monthName = "September"; break;
case 10: monthName = "October"; break;
case 11: monthName = "November"; break;
case 12: monthName = "December"; break;
default: monthName = "Invalid Month";
invalidInput = true; // set flag
} // end switch
if( !invalidInput )
{
// WRITE A SWITCH STATEMENT HERE TO ASSIGN to daysInMonth
// based on the input month number how many days
// are in the month (for example, 31 for 1, 30 for 4)
// USE 28 for 2 (February)
// WRITE ONE (NOT MORE) IF STATEMENT WITH LOGICAL OPERATORS
// WHICH WILL TEST IF the input day is between
// 1 and the daysInMonth, inclusive or not
// and assign true to the invalidInput flag if NOT
} // end if
if( invalidInput )
cout<<"Invalid date entered\n";
else
cout<<monthName<<" "<<day<<endl;
return 0;
}// end main
bool invalidInput;// true if input is invalid, false otherwise
I find that code is easier to understand if booleans always represent the positive sense of the world. In other words, I'd do: bool validInput; // true if input is valid, false otherwise
Now you can say if (validInput) ....
instead of if (!invalidInput)....
It doesn't make much difference in this example, but if the conditions in your code get complicated, it can make a big different.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
case 1: monthName = "January"; break;
case 2: monthName = "February"; break;
case 3: monthName = "March"; break;
case 4: monthName = "April"; break;
case 5: monthName = "May"; break;
case 6: monthName = "June"; break;
case 7: monthName = "July"; break;
case 8: monthName = "August"; break;
case 9: monthName = "September"; break;
case 10: monthName = "October"; break;
case 11: monthName = "November"; break;
case 12: monthName = "December"; break;
default: monthName = "Invalid Month";
invalidInput = true; // set flag
It sounds like this is an assignment, but I'll point out that it would make a lot more sense to do this as an array lookup instead.