Write a C++ program to validate whether a user keys in a valid birth date. Using the assert() function, validate that the day, month and year entered, fall within the valid ranges. Take care of leap years. Display to the user how old he will be this year, and whether he has been born in a leap year or not. Test you program with the following input, and submit the output for all three dates:29 2 1958 9 3 1958 20 10 2004 NB: Note that you are expected to use the assert macro in this question
#include <iostream>
#include <cassert>
using namespace std;
bool checkDate(int month, int day, int year);
bool isLeapYear(int year);
int main()
{
int d, m, y;
cout << "Enter DOB: ";
cin >> d >> m >> y;
assert(checkDate(m, d, y) && "Wrong date!");
return 0;
}
bool checkDate(int month, int day, int year)
{
if (year <= 1900)
return false;
if (1 > month && month > 12)
return false;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (1 > day && day > 31)
return false;
break;
case 4:
case 6:
case 9:
case 11:
if (1 > day && day > 30)
return false;
break;
case 2:
if (isLeapYear(year))
{
if (1 > day && day > 29)
return false;
}
else
{
if (1 > day && day > 28)
return false;
}
}
cout << "In this year you will be: " << 2021 - year << " years!" << endl;
if (isLeapYear(year)) cout << "And you was born in leap year!" << endl;
else cout << "And you was born in not leap year!" << endl;
return true;
}
bool isLeapYear(int year)
{
if (((year % 4 == 0) && (year % 100 != 0)) || year % 400 == 0)
return true;
else
return false;
}
Comments
Leave a comment