I need help getting my program to run when the user enters in one of the two choices to print out the results. Any suggestions on how to add my user prompts?
Prompt:
Create a Date class with the following capabilities:
a) Output the date in multiple formats such as
MM/DD/YYYY
June 14, 1992
b) Use overloaded constructors to create Date objects initialized with dates of the formats in part a).
You should only have 3 member data: integer month, integer day, and integer year. Also, you should have at least 2 constructors and 2 functions in your Date.cpp.
Code:
Date::Date ( int mm, int dd, int yy )
{ // check mm, dd, and yy and save them into month, day, and year member data.
…
}
Date::Date ( string mstr, int dd, int yy )
{ // check and convert mstr, dd, and yy and save them
// into month, day, and year member data.
…
}
void Date::print ( )
{ // print the date in MM/DD/YYYY
…
}
void Date::printFullDate ( )
{ // print the date in long date such as
// June 24, 2002
…
}
/////////////
You also need to create 3 get functions and 3 set functions for the member data.
Requirements:
1. 3 files: Date.h, Date.cpp-> class implementation, test.cpp-> test file
Sample Run:
3 choices for the user to pick:
Enter 1 for format: mm/dd/yyyy
Enter 2 for format: month dd, yyyy
Enter 3 to exit:
Choice: 1 //user input
//user inputs the information
Enter month (1-12): 1
Enter day of month: 5
Enter year: 2005
Mm/dd/yyyy: 1/5/2005 //gets checked for valid date.
//////////////
Enter 1 for format: mm/dd/yyyy
Enter 2 for format: month dd, yyyy
Enter 3 to exit:
Choice:2
Enter month name: May
Enter day of month: 10
Enter year:2010
Month dd, yyyy: May 10, 2010
Enter 1 for format: mm/dd/yyyy
Enter 2 for format: month dd, yyyy
Enter 3 to exit:
Choice:3
//////
Here is the code I have so far:
date.h
Code:
#indef DATE_H
#define DATE_H
#include <string>
using std::string;
class Date
{
public:
DATE();
DATE(int,int);
DATE(string,int,int);
void setDay(int);
void setMonth(int);
void print()const;
void printDDDYYYY() const;
void printMMDDYY() const;
void printMonthDDYYYY() const;
~Date();
private:
int month;
int day;
int year;
int chckDay(int) const;
int daysInMonth(int) const;
bool isLeapYear() const;
void setMMDDFromDDD(int);
string convertMMToMonth(int) const;
void setMMFromMonth(string);
int convertYYYYToYY() const;
void setYYYYFromYY(int);
};
#endif
}
date.cpp
Code:
#include <iostream>
using std::cout;
using std::endl;
#include<iomanip>
using std::setw;
using std::setfill;
#include <ctime>
using std:;time;
using std::localtime;
using std::tm;
using std::tim_t;
#include "date.h"
Date::Date()
{
struct tm *ptr;
time_t t=time(0);
ptr=localtime(&t);
day=ptr->tm_mday;
month=1+ptr->tm_mon;
year=ptr->tm_year +1900;
}
Date::Date(int mm,int dd,int yy)
{
setYYYYFromYY(yy);
setMonth(mm);
setDay(dd);
}
//montName ->mstr
Date::Date(string mstr, int dd, int yyyy)
{
setMMFromMonth(mstr);
setDay(dd);
year=yyyy;
}
void Date::setDay(int d)
{
day=checkDay(d);
}
void Date::setMonth(int m)
{
if(m>0 && m<=12)
month=m;
else{
month=1;
cout<<"Invalid month";
}
}
void Date::print() const
{
cout << month <<'/'<<day<<'/'<<year<<endl;
}
void Date::printMMDDYY() const
{
cout <<setw(2)<< setfill('0')<<month<<'/'<<setw(2)
<<setfill('0')<<day<<'/''
<<setw(2)<<setfill('0')<<convertYYYYToYY()<<endl;
}
void Date::printMonthDDYYYY()) const
{
cout<<convertMMtoMonth(month)<<''<<day<<","<<year<<endl;
}
Date::~Date()
{
cout<<"Date object destructor";
print();
cout<<endl;
}
int Date::checkDay(int testDay) const
{
if(testDay >0 && testDay<=daysInMonth(month))
return testDay;
if(month==2 && testDay==29 && isLeapYear())
return testDay;
cout<<"invalid";
}
int Date:;daysInMonth(int m) const
{
if(isLeapYear() && m==2)
return 29;
static const int daysPerMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
return daysPerMonth[m];
}
bool Date::isLeapYear() const
{
if(year%400==0 || (year % 4 ==0 && year %100!=0))
return true;
else
return false;
}
int Date::convertDDtoDDD() const
{
int ddd=0;
for(int i=1;i<month;i++)
ddd+=daysInMonth(i);
ddd+=day;
return ddd;
}
void Date::setMMDDFromDDD(int ddd)
{
int dayTotal=0;
int m;
for(m=1;m<=12 && (dayTotal +daysInMonth(m))<ddd;m++)
dayTotal+=daysInMonth(m);
setMonth(m);
setDay(ddd-dayTotal);
}
string Date::convertMMToMonth(int mm) const
{
static const string months[]=
{"", "January","February", "March", "April","May","June","July","August",
"September","October", "November", "December"};
return months[mm];
}
void Date::setMMFromMonth(string m)
{
bool matchFound=false;
for(int i=1;i<=12 && !matchFound;i++)
{
string tempMonth=convertMMToMonth(i);
if(tempMonth==m)
{
setMonth(i);
matchFound=true;
}
}
if(!matchFound)
{
cout<<"Invalid month name";
setMonth(1);
}
}
int Date::convertYYYYToYY() const
{
return(year>=2000 ? year-2000: year-1900);
}
void Date::setYYYYFromYY(int yy)
{
year=(yy<7 ? yy+2000: yy +1900);
}
test.cpp
Code:
#include "date.h"
int main()
{
Date date1(256,1999);
Date date2(3,25,04);
Date date3("September",1,2000);
Date date4;
date1.print();
date2.print();
date3.print();
date4.print();
cout << '\n';
date1.printMMDDYY();
date2.printMMDDYY();
date3.printMMDDYY();
date4.printMMDDYY();
cout<<'\n';
date1.printMonthDDYYYY();
date2.printMonthDDYYYY();
date3.printMonthDDYYYY();
date4.printMonthDDYYYY();
cout<<endl;
return 0;
}