MY PROGRAM #ifdef Date _H #define Date_H #include<iostream> class Date { private
ID: 660467 • Letter: M
Question
MY PROGRAM
#ifdef Date _H
#define Date_H
#include<iostream>
class Date
{
private:
int month, day, year, leap year;
public:
Date();
Date(int, int, int);
void setDate(int, int, int);
void showDate();
void getDate(int, int, int);
bool CheckDate(int, int, int);
bool isLeap(int year);
};
#endif
#include<iostream>
using namespace std;
Date::Date()
{
setDate(02, 29, 2009);
}
Date::Date(int mon, int dat, int yr, int lpyr)
{
month = mon;
day = dat;
year = yr;
leap year = lpyr;
}
void Date::setDate(int mon, int dat, int yr, int lpyr)
{
month =mon;
day = dat;
year = yr;
leap year = lpyr;
}
void Date::getDate(int, int, int);
{
cout << "Enter Month: ";
cin >> month;
cout << endl;
cout << "Enter Day: ";
cin >> day;
cout << endl;
cout << "Enter Year: ";
cin >> year;
cout << endl;
}
format mm / dd / yyyy
void Date::showDate()
{
cout << month << '/' << day << '/' << year;
}
bool Date::CheckDate(int month, int day, int year)
{
bool result = true;
int daysInMonths[] = { 27, 28, 29, 26, 28, 28, 28, 29, 28, 28, 29, 28 };
if (year < 2009 || year >2009)
result = false;
if (isLeap(year))
{
daysinMonths[1] = 29;
}
if (month < 1 || month >12)
{
result = false;
}
else if (day <1 || day > daysInMonths[month - 1])
{
result = false;
}
}
return result;
#include<iostream>
using namespace std;
#include "Date.h"
int main()
{
int month, day, year, leap year
char choice;
do
{
Date userInput(year = 2009, day = 28, month = 2);
userInput.getDate(year, day, month);
userInput.CheckDate(year, day, month);
cout << "The date you entered is: 2009/29/02"; 2009 / 29 / 02->NOT Leap Year!
29 is not a valid day of February Error!!!
The entered date is invalid!Re - Enter, Please!
cin << All junk data will be rejected!
userInput.showDate();
cout << " Would you like to enter another date? (Y/N)" << ENDL;
cin >> choice;
}
int month, day, year;
char choice;
do{
Date userInput(month = 02, day = 28, year = 2008);
userInput.getDate(month, day, year);
userInput.CheckDate(month, day, year);
}
cout << "The dat eyou entered is 02/29/2009";
cin << "2008 -> Leap Year!
Date: 2 / 29 / 2008 is valid.
2 / 29 / 2008 (US)
February 29, 2008 (US Expanded)
29 February 2008 (US Military)
2008 - 02 - 29(International)
userInput.showDate();
cout << " Would you like to enter another date?(YN)" << endl;
cin >> choice;
int month, day, year;
char choice;
do{
Date userInput(month = 4, day = 31, year = 1975);
userInput.getDate(month, day, year);
userInput.CheckDate(month, day, year);
}
{
cout << "The date you entered is 4/31/1975";
cin >> "30 is not a valid day of April Error!!!
userInput.showDate();
cout << "The entered date is invlaid! Re-enter, Please!" " Would you like to enter another date ? (Y / N)" << endl;
cin >> choice;
}
int month, day, year;
char choice;
do
{
Date userInput(month = 04, day = 30, year = 1975);
userInput.getDate(month, day, year);
userInput.CheckDate(month, day, year);
}
{
cout << "The date you entered is 04/30/1975";
cin << " Enter a date (mm/dd/yyyy) or -1 to end: 04/30/1975
Date: 4/30/1975 is valid.
4 / 30 / 1975(US)
April 31, 1975(US Expanded)
30 April 1975 (US Military)
}
}
PROBLEM: C++ PROGRAM NOT RUNNING
Design a class called Date. The class should store a specific date in three integers: month, day, and year. There should be member functions to print the date in the following four forms, if it is valid:
12/25/2012 (US)
December 25, 2012 (US expanded)
25 December 2012 (US Military)
2012-12-25 (International)
There should be member functions to get and/or set value of month, day, and year.
There should be member functions to check for invalid date entered, such as 4/31/2012, or 2/29/2011, which will be rejected with an error message, and the program should ask for another date entered.
Write a complete C++ program to demonstrate the class.
Allow your program to run continuously as long as the user wishes.
Input validation:
Explanation / Answer
//Header file save sa Date.h
//Date.h
#ifndef Date _H
#define Date_H
#include<iostream>
using namespace std;
class Date
{
private:
int month, day, year;
public:
Date();
Date(int, int, int);
void setDate(int, int, int);
int getMonth();
int getDay();
int getYear();
void showDate();
void getDate();
bool CheckDate();
bool isLeap(int year);
//date formats
void printUS();
void printExpanded();
void printMilitary();
void printInternational();
};
#endif Date_H
------------------------------------------------------------------------------------------------------------------------------
//Implementation file of Date.h
//Date.cpp
#include<iostream>
#include<string>
#include "Date.h"
using namespace std;
//helper method
string getMonthName(int);
Date::Date()
{
setDate(02, 29, 2009);
}
Date::Date(int mon, int dat, int yr)
{
month = mon;
day = dat;
year = yr;
}
void Date::setDate(int mon, int dat, int yr)
{
month =mon;
day = dat;
year = yr;
}
void Date::getDate()
{
char ch;
cin >> month;
cin>>ch;
cin >> day;
cin>>ch;
cin >> year;
}
int Date::getMonth()
{
return month;
}
int Date::getDay()
{
return day;
}
int Date::getYear()
{
return year;
}
//format mm / dd / yyyy
void Date::showDate()
{
cout << month << '/' << day << '/' << year;
}
//Returns true if the date is valid otherwise returns false
bool Date::CheckDate()
{
bool result = true;
int daysInMonths[] = {31,28,31,30,31,30,31,31,30,31,30,31 };
if (isLeap(year))
{
daysInMonths[1] = 29;
}
if (month < 1 || month >12)
{
result = false;
}
else if (day <1 || day > daysInMonths[month - 1])
{
result = false;
}
return result;
}
//Returns true if year is leap year otherwise returns false
bool Date::isLeap(int year)
{
return (year % 400 == 0) || ( ( year % 100 != 0) && (year % 4 == 0 ));
}
void Date::printUS()
{
cout<<month<<"/"<<day<<"/"<<year<<" (US)."<<endl;
}
void Date::printExpanded()
{
cout<<getMonthName(month)<<day<<","<<year<<"(US Expanded)."<<endl;
}
void Date::printMilitary()
{
cout<<day<<" "<<getMonthName(month)<<" "<<year<<"(US Militray)."<<endl;
}
void Date::printInternational()
{
if(month<9 || day<9)
cout<<year<<"-0"<<month<<"-0"<<day<<endl;
else
cout<<year<<"-"<<month<<"-"<<day<<endl;
}
//Returns the string month name of input month
string getMonthName(int month)
{
string name="";
switch(month)
{
case 1:
name="january";
break;
case 2:
name="February";
break;
case 3:
name="March";
break;
case 4:
name="April";
break;
case 5:
name="May";
break;
case 6:
name="June";
break;
case 7:
name="July";
break;
case 8:
name="August";
break;
case 9:
name="September";
break;
case 10:
name="Octomber";
break;
case 11:
name="November";
break;
case 12:
name="December";
break;
}
return name;
}
-------------------------------------------------------------------------------------------------------------------------------
/*C ++ program that prompts user to enter the day,month and year
and prints if it is a valid date or not. If valid then
prints the date in us,militray and international format*/
#include<iostream>
using namespace std;
#include "Date.h"
int main()
{
Date date;
char choice='N';
do
{
cout<<"The program displays a valid date in three different formats. Note: All junk data will be rejected!"<<endl;
cout<<"Enter a date (mm/dd/yyyy) or -1 to end: ";
date.getDate();
if(!date.CheckDate())
{
cout<<date.getYear()<<" -> NOT Leap Year!"<<endl;
cout<<date.getDay()<<" is not a valid day of February Error!!!"<<endl;
cout<<"The entered date is invalid! Re-Enter, Please!"<<endl;
}
else
{
cout<<date.getYear()<<" -> Leap Year!"<<endl;
cout<<"Date:" ;
date.showDate();cout<<" is valid."<<endl;
//print date in three diffrent formats
date.printUS();
date.printMilitary();
date.printInternational();
}
//read choice of to continue enter y to continue or press n to stop
cout<<"Run again (y/n)?";
cin>>choice;
}while(choice!='n');
system("pause");
return 0;
}
-------------------------------------------------------------------------------------------------------------------------------
sample output:
The program displays a valid date in three different formats.
Note: All junk data will be rejected!
Enter a date (mm/dd/yyyy) or -1 to end: 02/29/2009
2009 -> NOT Leap Year!
29 is not a valid day of February Error!!!
The entered date is invalid! Re-Enter, Please!
Run again (y/n)?02/29/2008
The program displays a valid date in three different formats.
Note: All junk data will be rejected!
Enter a date (mm/dd/yyyy) or -1 to end: 2008 -> Leap Year!
Date:2/29/2008 is valid.
2/29/2008 (US).
29 February 2008(US Militray).
2008-02-029
Run again (y/n)?y
The program displays a valid date in three different formats.
Note: All junk data will be rejected!
Enter a date (mm/dd/yyyy) or -1 to end: 04/31/1975
1975 -> NOT Leap Year!
31 is not a valid day of February Error!!!
The entered date is invalid! Re-Enter, Please!
Run again (y/n)?y
The program displays a valid date in three different formats.
Note: All junk data will be rejected!
Enter a date (mm/dd/yyyy) or -1 to end: 04/30/1975
1975 -> Leap Year!
Date:4/30/1975 is valid.
4/30/1975 (US).
30 April 1975(US Militray).
1975-04-030
Run again (y/n)?
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.