Question: C++ Write a program to validate date. The program will prompt user to enter a date in the format of mm/dd/yyyy. The entered date is
C++ Write a program to validate date. The program will prompt user to enter a date in the format of mm/dd/yyyy. The entered date is validated for month, day and year, and a message is displayed regarding whether the date is valid. The program will keep on running as long as user do not enter end-of-file as input (
Here are the rules that must be followed to verify a date.
year, yyyy, must be greater than 999 and less than 10,000
month, mm, must be between 1 & 12
day, dd, must be valid for the month and year.
month of 1, 3, 5, 7, 8, 10 and 12 must have days between 1-31
month of 4, 6, 9, and 11 must have days between 1-30
month of February, 2, has 28 days if it is NOT a leap year
has 29 days if it IS a leap year
A leap year is generally thought of as being any year divisible by 4. However, this is not always true. The exception is century years (divisible by 100). A century year is a leap year only if it is also divisible by 400.
Example:
A non-century is a year that is not divisible by 100. A non-century is a leap year if it is divisible by 4:
Year 1996 1996 % 4 == 0 IS leap year
Year 1991 1991 % 4 != 0 IS NOT leap year
A century year is always divisible by 4. It is leap year only if it is also divisible by 400.
Year 2000 2000 % 400 == 0 it IS a leap year
Year 1400 1400 % 400 != 0 it is NOT a leap year
The main function should call three functions to help do its job. The following are the function prototypes:
void getDate( int&, int&, int&); // get month, day and year from user and store them
// in reference arguments sent by caller
int ckDate( int, int, int); // check date based on three int values sent by caller
// return an int indicating the status of the date
void displayMsg( int); // display a message based on the date status
Your ckDate function should return an integer value depending on the status of the date being ckecked:
Value Message
0 Good Date
1 Bad Year
2 Bad Month
3 Bad Day not 1-31
4 Bad Day not 1-30
5 Bad Day not 1-29
6 Bad Day not 1-28
Based on the return value of ckDate(), the corresponding message is displayed by the displayMsg() function.
Note:
It is important to figure out the algorithm before coding. When a program uses multiple functions, the algorithm is divided into levels. If the main calls ckDate function, then the algorithm for main is simply check the date (call ckDate) and dont need mention about how to check a date. The detail of how to check a date would be the algorithm of ckDate function. The analysis of chapter 6 examples should be very helpful.
When using sub-functions, the algorithm should be expressed in levels, where level 0 reflects whats to be done in main, and level1 outlines what is done in the function that is called in main, so on so forth. Place the spec at the top of the code as comments. The spec should include narrative, input, output, constants and constraints, and leveled algorithm. For the algorithm part, you only need do level 0 and the one for function ckDate.
Note: Do not use any global variables!
Use meaningful and convenient variable names, proper indentation, appropriate comments, and good prompting messages. You are graded on both correctness and program style.
Note, dont enter the leading 0 in case month or day is single digit, even the prompt asks for mm/dd/yyyy. Below is a sample run:
Enter a date (mm/dd/yyyy): 1/2/1998
1/2/1998 - Good Date
Enter a date (mm/dd/yyyy): 14/2/2000
14/2/2000 - Bad Month
Enter a date (mm/dd/yyyy): 2/33/1100
2/33/1100 - Bad Day not 1-28
Enter a date (mm/dd/yyyy): Ctr+Z
Good Bye!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
