Need help with Switch Statement!

I just started learning how to use switch statement. Could u guy help me complete this code pls?

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
 #include <iostream>
using namespace 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
switch(monthName)
{
		case "January": daysInMonth=31; break;
		case "February": daysInMonth=28; break;
		case "March": daysInMonth=31; break;
		case "April": daysInMonth=30; break;
		case "May": daysInMonth=31; break;
		case "June": daysInMonth=30; break;
		case "July": daysInMonth=31; break;
		case "August": daysInMonth=31; break;
		case "September": daysInMonth=30; break;
		case "October": daysInMonth=31; break;
		case "November": daysInMonth=30; break;
		case "December": daysInMonth=31; break;
		default: daysInMonth=0;
}




1
2
3
4
if( day>=1  &&  day<= daysInMonth )
invalidInput=0;
else 
invalidInput=1;
Your first switch changes monthName based on month.
Your second switch should change daysInMonth based on month.
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.
Topic archived. No new replies allowed.