PDA

View Full Version : C++ switch statement



Epgs
October 15th, 2002, 10:15
How would I use a switch statement to calculate the number of days in a month?

Dusty
October 15th, 2002, 11:27
This assumes 1=January and 12=December. It doesn't take into account the days in February, I'll assume you've already calculated whether it's a leap year or not and stored that in a variable to itself. If the calender doesn't really matter, seeing if the year's divisible by four may be sufficient; but if it is, finding the true leap years is a helluva lot more complicated than that.
int days;
int febdays=28;
int month;
switch month{
case 4: case 6: case 9: case 11:
days=30;
break;
case 2:
days=febdays;
break;
default:
days=31;
}