I need this code to allow for user input to calculate the days passed and days l
ID: 3772543 • Letter: I
Question
I need this code to allow for user input to calculate the days passed and days left in the year. It needs to accept a valid date and check for leap year.
#include<iostream>
using namespace std;
class Date
{
public:
Date (int = 1, int = 1, int = 1990); //default constructor with parameters
void print() const;
void setDate(int m,int d,int y);
void nextDay();
int daysPassed();
int daysLeft();
void printLongDate();
private:
int month;
int day;
int year;
bool isLeap(int);
int daysInMonth(int month_num);
int presentDay;
};
Date::Date(int m, int d, int y)
{
month = m;
day = d;
year = y;
}
void Date::setDate(int m,int d,int y)
{
if(m < 1 || m > 12 )
month = 1;
else
month = m;
if(d < 1 || d > daysInMonth(month))
day = 1;
else
day = d;
if(y < 1)
year = 1990;
else
year = y;
}
void Date::print() const
{
cout << month << "-" << day << "-" << year;
}
bool Date::isLeap(int Year)
{
return (!(year % 4) && (year % 100)) || !(year % 400);
}
int Date::daysInMonth(int month_num)
{
int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(isLeap(year))
days[1]++;
return days[month_num - 1];
}
void Date::nextDay()
{
if(day < daysInMonth(month))
day++;
else if(day == daysInMonth(month) && month != 12 && month)
{
month++;
day = 1;
}
else if(day == daysInMonth(month) && month == 12)
{
year++;
month = 1;
day = 1;
}
}
int Date::daysPassed()
{
int days_passed = 0;
for(int i = 1; i < month; i++)
{
days_passed = days_passed + daysInMonth(i);
}
return days_passed + day;
}
int Date::daysLeft()
{
if(isLeap(year)) return 366 - daysPassed();
return 365 - daysPassed();
}
void Date::printLongDate()
{
string month_names[] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
cout << month_names[month - 1] << " " << day << ", " << year <<endl;
}
int main()
{
// take invalid date and print it;
Date invalid_date;
// below code checks setDate function and sets date to default date.
invalid_date.setDate(31, 32, -31);
// print the invalid date.
cout << "Invalid Date is: ";
// below code test the print function.
invalid_date.print();
cout << endl;
Date local_date(2, 55, 2015);
cout << "Current Date is: ";
// below code test printLongDate function.
local_date.printLongDate();
//below code test daysPassed function.
cout << "Number of days passed from beginning of year is: " << local_date.daysPassed() << endl;
//below code test daysLeft function.
cout << "Number of days left in year: " << local_date.daysLeft() << endl;
return 0;
}
Explanation / Answer
#include<iostream.h>
#include<conio.h>
//using namespace std;
class Date
{
public:
Date (int = 1, int = 1, int = 1990); //default
constructor with parameters
void print() const;
void setDate(int m,int d,int y);
void nextDay();
int daysPassed();
int daysLeft();
void printLongDate();
private:
int month;
int day;
int year;
bool isLeap(int);
int daysInMonth(int month_num);
int presentDay;
};
Date::Date(int m, int d, int y)
{
month = m;
day = d;
year = y;
}
void Date::setDate(int m,int d,int y)
{
if(m < 1 || m > 12 )
month = 1;
else
month = m;
if(d < 1 || d > daysInMonth(month))
day = 1;
else
day = d;
if(y < 1)
year = 1990;
else
year = y;
}
void Date::print() const
{
cout << month << "-" << day << "-" << year;
}
bool Date::isLeap(int Year)
{
return (!(year % 4) && (year % 100)) || !(year %
400);
}
int Date::daysInMonth(int month_num)
{
int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31};
if(isLeap(year))
days[1]++;
return days[month_num - 1];
}
void Date::nextDay()
{
if(day < daysInMonth(month))
day++;
else if(day == daysInMonth(month) && month != 12
&& month)
{
month++;
day = 1;
}
else if(day == daysInMonth(month) && month ==
12)
{
year++;
month = 1;
day = 1;
}
}
int Date::daysPassed()
{
int days_passed = 0;
for(int i = 1; i < month; i++)
{
days_passed = days_passed + daysInMonth(i);
}
return days_passed + day;
}
int Date::daysLeft()
{
if(isLeap(year)) return 366 - daysPassed();
return 365 - daysPassed();
}
void Date::printLongDate()
{
string month_names[] =
{"January","February","March","April","May","June","Ju
ly","August","September","October","November","Decem
ber"};
cout << month_names[month - 1] << " " << day <<
", " << year <<endl;
}
int main()
{
int mon, da, ye;
// take invalid date and print it;
Date invalid_date;
// below code checks setDate function and sets date
to default date.
cout<<"Enter month";
cin>>mon;
cout<<"Enter day";
cin>>da;
cout<<"Enter Year";
cin>>ye;
invalid_date.setDate(mon, da, ye);
// print the invalid date.
cout << "Invalid Date is: ";
// below code test the print function.
invalid_date.print();
cout << endl;
Date local_date(mon, da, ye);
cout << "Current Date is: ";
// below code test printLongDate function.
local_date.printLongDate();
//below code test daysPassed function.
cout << "Number of days passed from beginning of
year is: " << local_date.daysPassed() << endl;
//below code test daysLeft function.
cout << "Number of days left in year: " <<
local_date.daysLeft() << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.