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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
/*
Write a program that checks the validity of a date. The date is entered as separate numbers for
the month, day and year.
For example, if the date was March 16th, 2008, the user would enter 3 followed by 16 followed
by 2008. The program checks that:
• the month is a valid number for a month
• the number of days in the month is valid for that month.
• If the user entered February 29th
, you must check if the the year is a valid leap year.
The program just outputs the date and tells the user if the date is valid or not.
The program contains four functions:
• The main function.
• A function that gives instructions to the user about how to enter the date
• A function that is sent a month and a day and returns a Boolean value to represent
whether that is a valid day for that month. This function should use a switch statement.
• The leap year function that determines if this is a leap year. This is your leap year
function from last week.
*/
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
// declaring
int Day = 0;
int Month = 0;
int Year = 0;
bool leapYear (int);
int main(int argc, char *argv[])
{
// outputting messages to the user
cout << "Lets check your dates shall we?" << endl;
cout << "Please enter in a two(2) digit number for the month." << endl;
cin >> Month;
cout << "Please enter a number for the day (1 - 12)." << endl;
cin >> Day;
cout << "Please enter in a four(4) digit number for the year." << endl;
cin >> Year;
switch(Month)
{
case 1:
if (Day>=1 && Day<=31)
cout << "January" << Day << "th," << Year << "," << endl;
break;
case 2:
if (Day>=1 && Day<=28)
cout << "February" << Day << "th," << Year << "," << endl;
else if (Day == 29)
leapYear(Year)
cout << "February" << Day << "th," << Year << "," << "Is also a leap year!" endl;
else
cout << "Invalid date." << endl;
break;
case 3:
if (Day>=1 && Day<=31)
cout << "March" << Day << "th," << Year << "," << endl;
break;
case 4:
if (Day>=1 && Day<=30)
cout << "April" << Day << "th," << Year << "," << endl;
break;
case 5:
if (Day>=1 && Day<=31)
cout << "May" << Day << "th," << Year << "," << endl;
break;
case 6:
if (Day>=1 && Day<=30)
cout << "June" << Day << "th," << Year << "," << endl;
break;
case 7:
if (Day>=1 && Day<=31)
cout << "July" << Day << "th," << Year << "," << endl;
break;
case 8:
if (Day>=1 && Day<=31)
cout << "August" << Day << "th," << Year << "," << endl;
break;
case 9:
if (Day>=1 && Day<=30)
cout << "September" << Day << "th," << Year << "," << endl;
break;
case 10:
if (Day>=1 && Day<=31)
cout << "October" << Day << "th," << Year << "," << endl;
break;
case 11:
if (Day>=1 && Day<=30)
cout << "November" << Day << "th," << Year << "," << endl;
break;
case 12:
if (Day>=1 && Day<=31)
cout << "December" << Day << "th," << Year << "," << endl;
break;
default:
cout << "Invalid date." << endl;
break;
}
system("PAUSE");
return EXIT_SUCCESS;
}
// leap year bool
bool leapYear (int)
{
if ((( Year % 4 == 0) && (! ( Year % 100 == 0))) || (( Year % 4 == 0) && (! ( Year % 100 == 0))&&( Year % 400 == 0)))
return true;
else
return false;
}
| |