Author Topic: check if date input is valid  (Read 3458 times)

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
check if date input is valid
« on: February 25, 2006, 02:57:51 PM »
A good demo of how to use switch and if-else structures is provided in the exercise problem involving month and day values:

Code: [Select]

// Tests if month and day values are in the correct range.  
// For February the value allows up to 29. Since the year info
// is not available, check of valid dates during a leap year
// is not done.
bool isDate(int m, int d)
{
bool result;
if (m>0 && m<13)
{
switch (m)
{
case 2:
if (d>0 && d<=29) result = true;
else result = false;
break;
case 4:
case 6:
case 9:
case 11:
if (d>0 && d<=30) result = true;
else result = false;
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (d>0 && d<=31) result = true;
else result = false;
break;
default:
result = false;
}
}
else
{
result = false;
}
return result;
}
Analyze. Design. Develop. Debug. Deploy. Maintain.