C++ Program: Design a class called Date. The class should store a specific date
ID: 3673816 • Letter: C
Question
C++ Program:
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:
Do not accept values for the day greater than 31 or less than 1.
Do not accept values for the month greater than 12 or less than 1.
Do not accept any invalid date. If it is, display the message “The date is invalid”.
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: 2/29/2008
Invalid date/ wrong format: For both month and day, use two digits. Re-Enter a date (mm/dd/yyyy) or -1 to end: 02/29/2009
2009 -> NOT Leap Year!
29 is not a valid day of Febraury
Error!!! The entered date is invalid! Re-Enter, Please! Enter a date (mm/dd/yyyy) or -1 to end: 02/29/2008
2008 -> Leap Year! Date: 2/29/2008 is valid.
2/29/2008 (US).
Febraury 29, 2008 (US Expanded). 29 Febraury 2008 (US Military). 2008-02-29 (International).
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: 4/31/1975
Invalid date/ wrong format: For both month and day, use two digits. Re-Enter a date (mm/dd/yyyy) or -1 to end: 04/31/1975
31 is not a valid day of April
Error!!! The entered date is invalid! Re-Enter, Please!
Enter a date (mm/dd/yyyy) or -1 to end: 04/30/1975 Date: 4/30/1975 is valid.
4/30/1975 (US).
April 30, 1975 (US Expanded). 30 April 1975 (US Military). 1975-04-30 (International).
Run again (y/n)? n
Press <Enter> key to end ...
somebody posted the following but it won't run, it keeps giving me these two errors:
error LNK1169: one or more multiply defined symbols found
error LNK2005: _main already defined in Source.obj
can you please change the code or give me a new code that works. thank you.
#include<stdio.h>
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
class Date
{
public:
int dd;
int mm;
int yy;
public:
void setDate(int dd, int mm, int yy)
{
this->dd=dd;
this->mm=mm;
this->yy=yy;
}
bool isValid(int dd, int mm, int yy)
{
if(mm==1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12)
{
if(dd>31||dd<1)
{
return false;
}
else
return true;
}
else if(mm==2)
{
if((dd>28)&&(!leapYear(yy)))
{
cout<<" "<<yy<<" -> Not a Leap Year!";
cout<<dd<<" is not a valid date of February!"<<endl;
return false;
}
else
return true;
}
else
{
if(dd>30||dd<1)
{
return false;
}
else
return true;
}
}
int leapYear(int yr)
{
if(yr%4 == 0)
{
if( yr%100 == 0)
{
if ( yr%400 == 0)
return 1;
else
return 0;
}
else
return 1;
}
else
return 0;
}
};
int main()
{
int dd,mm,yy;
string dateInp;
while(true)
{
cout << "Enter a date (mm/dd/yyyy): " << endl;
cin >> dateInp;
if(dateInp.compare("-1"))
break;
mm = stoi(dateInp.substr(0,2));
dd = stoi(dateInp.substr(3,2));
yy = stoi(dateInp.substr(6));
Date date;
if(date.isValid(dd,mm,yy))
{
date.setDate(dd,mm,yy);
}
else
{
printf(" Invalid Date!! Enter again!");
}
}
}
Explanation / Answer
Hi, I have corrected the code. It will give you desired result.
My program need C++ 11 version to compile.
so use g++ -std=c++0x file_name.cpp to compile in linux.
#include<stdio.h>
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
class Date
{
public:
int dd;
int mm;
int yy;
public:
void setDate(int dd, int mm, int yy)
{
this->dd=dd;
this->mm=mm;
this->yy=yy;
}
bool isValid(int dd, int mm, int yy)
{
if(mm==1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12)
{
if(dd>31||dd<1)
{
return false;
}
else
return true;
}
else if(mm==2)
{
if((dd>28)&&(!leapYear(yy)))
{
cout<<" "<<yy<<" -> Not a Leap Year!";
cout<<dd<<" is not a valid date of February!"<<endl;
return false;
}
else
return true;
}
else
{
if(dd>30||dd<1)
{
return false;
}
else
return true;
}
}
int leapYear(int yr)
{
if(yr%4 == 0)
{
if( yr%100 == 0)
{
if ( yr%400 == 0)
return 1;
else
return 0;
}
else
return 1;
}
else
return 0;
}
};
string getMonth(int m){
string Months[]={"January","February","March","April","May","June","July",
"August","September","October","November","December"};
return Months[m-1];
}
int main()
{
int dd,mm,yy;
string dateInp;
while(true)
{
cout << "Enter a date (mm/dd/yyyy): " << endl;
cin >> dateInp;
if(dateInp.compare("-1") == 0)
break;
mm = stoi(dateInp.substr(0,2));
dd = stoi(dateInp.substr(3,2));
yy = stoi(dateInp.substr(6));
Date date;
if(date.isValid(dd,mm,yy))
{
date.setDate(dd,mm,yy);
cout<<getMonth(date.mm)<<" "<<date.dd<<", "<<date.yy<<"(US Expanded)"<<endl;
cout<<date.dd<<" "<<getMonth(date.mm)<<" "<<date.yy<<"(US Military)"<<endl;
cout<<date.yy<<" "<<date.mm<<" "<<date.dd<<"(International)"<<endl;
}
else
{
printf(" Invalid Date!! Enter again!");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.