PLEASE DO IT IN C++. I have completed the problem BUT THE CODE DOES NOT CHECK FO
ID: 3604864 • Letter: P
Question
PLEASE DO IT IN C++. I have completed the problem BUT THE CODE DOES NOT CHECK FOR THE YEAR. YEAR HAS to BE in FOUR DIGITS. SEE THE FILES BELOW
main.cpp
#include <iostream>
#include "dateType.h"
using namespace std;
int main()
{
int Month, Day, Year;
char c;
while (true)
{
Month = 0; Day = 0; Year = 0;
cout << "Month: ";
cin >> Month;
cout << "Day: " ;
cin >> Day;
cout << "Year: ";
cin >> Year;
dateType dt;
if (dt.setDate(Month, Day, Year)){
dt.printDate();
cout << endl << "Leap year = " << dt.isLeapYear() << endl;
cout << "Press any key to continue...";
cin >> c;
//break;
}
else
cout <<endl<< endl<<"Please enter proper date format"<<endl;
}
return 0;
}
dateType.h
#ifndef dateType_H
#define dateType_H
class dateType
{
public:
// check the format of month, day and year.
// if the format is incorrect, displays a message and returns false
// if the format is correct, returns true
bool setDate(int month, int day, int year);
//determines whether its a leap year or not
bool isLeapYear() const;
// returns current day
int getDay() const;
// returns current month
int getMonth() const;
// returns current year
int getYear() const;
// prints the current date
void printDate() const;
dateType();
private:
int dMonth;
int dDay;
int dYear;
};
#endif
dateTypeImp.cpp
#include <iostream>
#include "dateType.h"
using namespace std;
bool dateType::setDate(int month, int day, int year)
{
if (month <= 12 && month != 0) {
dMonth = month;
}
else {
cout << "Month is not valid ";
return false;
}
int i = month;
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {
if (day <= 31 && day != 0) {
dDay = day;
}
else {
cout << "Day is not valid ";
return false;
}
}
else if (i == 4 || i == 6 || i == 9 || i == 11) {
if (day <= 30 && day != 0) {
dDay = day;
}
else {
cout << "Day is not valid ";
return false;
}
}
else {
int check = 0;
if (year % 4 == 0) {
check = 28;
}
else {
check = 29;
}
if (day <= check && day != 0) {
dDay = day;
}
else {
cout << "Day is not valid ";
return false;
}
}
dMonth = month;
dDay = day;
dYear = year;
}
int dateType::getDay() const
{
return dDay;
}
int dateType::getMonth() const
{
return dMonth;
}
int dateType::getYear() const
{
return dYear;
}
void dateType::printDate() const
{
cout << dMonth << "-" << dDay << "-" << dYear;
}
dateType::dateType()
{
dMonth = 1;
dDay = 1;
dYear = 1990;
}
bool dateType::isLeapYear() const
{
if (dYear % 4 == 0) {
return true;
}
else {
return false;
}
}
A basic class datelvpe is designed to implement the date in a program, but the member function setDate does not check whether the date is in valid format. Rewrite the definition of the function setDate so that the values for the month (1 to 12), day ( to 31), and year (4 digits) are checked before storing the date into the member variables. a. b. Add amember function, isLeapYear, to check whether a year is a leap year Sample Output onth: 13 ay: 25 ear: 2017 onth is not valid lease enter proper date format onth: 12 ay: 35 ear: 2016 ay is not valid date format lease enter proper onth: 12 y: 31 ear: 20017 ear is not valid lease enter proper date format onth: 12 ay: 31 ear: 2017 2-31-2017 eap year e ress any key to continueExplanation / Answer
#include <iostream>
#include "dateType.h"
using namespace std;
int main()
{
int Month, Day, Year;
char c;
while (true)
{
Month = 0; Day = 0; Year = 0;
cout << "Month: ";
cin >> Month;
cout << "Day: " ;
cin >> Day;
cout << "Year: ";
cin >> Year;
dateType dt;
if (dt.setDate(Month, Day, Year)){
dt.printDate();
cout << endl << "Leap year = " << dt.isLeapYear() << endl;
cout << "Press any key to continue...";
cin >> c;
//break;
}
else
cout <<endl<< endl<<"Please enter proper date format"<<endl;
}
return 0;
}
dateType.h
#ifndef dateType_H
#define dateType_H
class dateType
{
public:
// check the format of month, day and year.
// if the format is incorrect, displays a message and returns false
// if the format is correct, returns true
bool setDate(int month, int day, int year);
//determines whether its a leap year or not
bool isLeapYear() const;
// returns current day
int getDay() const;
// returns current month
int getMonth() const;
// returns current year
int getYear() const;
// prints the current date
void printDate() const;
dateType();
private:
int dMonth;
int dDay;
int dYear;
};
#endif
dateTypeImp.cpp
#include <iostream>
#include "dateType.h"
using namespace std;
bool dateType::setDate(int month, int day, int year)
{
if (month <= 12 && month != 0) {
dMonth = month;
}
else {
cout << "Month is not valid ";
return false;
}
int i = month;
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {
if (day <= 31 && day != 0) {
dDay = day;
}
else {
cout << "Day is not valid ";
return false;
}
}
else if (i == 4 || i == 6 || i == 9 || i == 11) {
if (day <= 30 && day != 0) {
dDay = day;
}
else {
cout << "Day is not valid ";
return false;
}
}
else {
int check = 0;
if (year % 4 == 0) {
check = 28;
}
else {
check = 29;
}
if (day <= check && day != 0) {
dDay = day;
}
else {
cout << "Day is not valid ";
return false;
}
}
dMonth = month;
dDay = day;
dYear = year;
}
int dateType::getDay() const
{
return dDay;
}
int dateType::getMonth() const
{
return dMonth;
}
int dateType::getYear() const
{
return dYear;
}
void dateType::printDate() const
{
cout << dMonth << "-" << dDay << "-" << dYear;
}
dateType::dateType()
{
dMonth = 1;
dDay = 1;
dYear = 1990;
}
bool dateType::isLeapYear() const
{
if (dYear % 4 == 0) {
return true;
}
else {
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.