#include<iostream> #include<cstring> #include<bits/stdc++.h> using namespace std
ID: 673236 • Letter: #
Question
#include<iostream>
#include<cstring>
#include<bits/stdc++.h>
using namespace std;
//class declaration
class date{
int month, day, year;
static const int date1[];
public:
void setdate(int m, int d, int y){
month = m;
day = d;
year = y;
}
date(){
month=1;
day=1;
year=1990;
}
bool leapyear(int);
bool end_of_month(int);
void nextday();
int count();
int day_left();
int days_month();
friend istream& operator >> (istream &,date &);
friend ostream& operator << (ostream &,date &);
};
void printopt();
int getopt();
const int date::date1[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main(){
date d1;
int year, month, day;
int opt, x;
while(1){
printopt();
opt = getopt();
cout << endl;
switch(opt){
case 0: exit(0);
case 1: cin >> d1;
cout << endl;
break;
case 2: d1.nextday();
cout << endl;
break;
case 3: cout << "Days since the begining of the year: " << d1.count() << endl << endl;
break;
case 4: cout << "Days left till the end of the year: " << 366-d1.count() << endl << endl;
break;
case 5: cout << d1.leapyear(year);
cout << endl << endl;
break;
case 6: cout << "Number of days in the month " << d1.days_month() << endl << endl;
break;
case 7: cout << d1 << endl << endl;
break;
default:
break;
}
}
return 0;
}
//List the main user choices
void printopt(){
cout << "0: Exit the program." << endl;
cout << "1: Enter in a date." << endl;
cout << "2: Print the date for tomorrow." << endl;
cout << "3: # of the days since the begining of the year." << endl;
cout << "4: Days left till the end of the year." << endl;
cout << "5: Is the current year a leap year?" << endl;
cout << "6: Usual number of days for the current month." << endl;
cout << "7: Print the current entered date." << endl;
}
//User enters main choice
int getopt(){
int opt;
cout << "Pick option 0-7: ";
cin >> opt;
if (opt > 7){
cout << "Inccorect input" << endl;
return 0;
}
return opt;
}
//Case: 1
istream& operator >> (istream &i, date &i1){
int y, m, d;
cout << "Enter the date, hitting enter after month, date and year." << endl;
i >> m >> d >> y;
i1.setdate(m, d, y);
}
//Case: 2
void date::nextday(){
if(month > 13)
cout << "invalid month" << endl;
else if(!end_of_month(day)){
day++;
cout << month << " : " << day << " : " << year << endl;
}
else if(month <= 12 && day < date1[month]){
day++;
cout << " Month: " << month << " Day: " << day << " Year: " << year << endl;
}
else if(day == date1[month] && month == 12){
month = 1;
day = 1;
year++;
cout << " Month: " <<month << " Day: " << day << " Year: " << year << endl;
}
else if(day == date1[month]){
month++;
day = 1;
cout << " Month: " << month << " Day: " << day << " Year: " << year << endl;
}
else{
year++;
month = 1;
day = 1;
cout << "Month: " << month << "Day: " << day << "Year: " << year << endl;
}
}
//Case: 3
int date::count(){
int days = 0;
for (int i = 1; i <= month-1; i++)
days += date1[i];
days += day;
if (leapyear(year) == false) days -= 1;
return days;
}
//Case: 4
int date::day_left(){
if (leapyear(year) == true)
return 366-count();
return 365-count();
}
//Case: 5 Leapyear Bool
bool date::end_of_month(int tday){
if(month == 2 && leapyear(year))
return tday = 29;
else
return tday;
}
bool date::leapyear(int year){
if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
return 1;
else
return 0;
}
//Case: 6 Private function that returnes the usual number of days in a given month (enter number 1-12)
int date::days_month(){
if (month == 2){
if (leapyear(year) == true) return 29;
return 28;
}
return date1[month];
}
//Case: 7
ostream& operator << (ostream &i, date &i1){
int y, m, d;
cout << "The date in Month Day, Year format is: ";
if (i1.month == 1)
cout << "January" << " " << i1.day << ", " << i1.year << endl << endl;
else if (i1.month == 2)
cout << "February " << i1.day << ", " << i1.year << endl << endl;
else if (i1.month == 3)
cout << "March " << i1.day << ", " << i1.year << endl << endl;
else if (i1.month == 4)
cout << "April " << i1.day << ", " << i1.year << endl << endl;
else if (i1.month == 5)
cout << "May " << i1.day << ", " << i1.year << endl << endl;
else if (i1.month == 6)
cout << "June " << i1.day << ", " << i1.year << endl << endl;
else if (i1.month == 7)
cout << "July " << i1.day << ", " << i1.year << endl << endl;
else if (i1.month == 8)
cout << "August " << i1.day << ", " << i1.year << endl << endl;
else if (i1.month == 9)
cout << "September " << i1.day << ", " << i1.year << endl << endl;
else if (i1.month == 10)
cout << "October " << i1.day << ", " << i1.year << endl << endl;
else if (i1.month == 11)
cout << "November " << i1.day << ", " << i1.year << endl << endl;
else
cout << "December " << i1.day << ", " << i1.year << endl << endl;
return i;
}
THE ABOVE CODE RUNS BUT DOES NOT PERFORM A COUPLE OF THINGS THAT IT NEEDS TO.
1. IN CASE 1 (ENTER IN A DATE, LINES 56/104) THERE NEEDS TO BE PROGRAMMING IN PLACE IN THE FUCTION TO PREVENT PEOPLE FROM ENTERING IN INCORRECT NUMBERS (SUCH AS 34 FOR THE MONTH) OR LETTERS/WORDS.
2. IN CASE 5 (IS THE CURRENT YEAR A LEAP YEAR, LINES 66/160/167) IT ONLY RETURNS THE NUMBERS 1 OR 0 IN THE OUTPUT. IT NEEDS TO RETURN SOMETHING LIKE "The current year is a leapyear." or "The current year is not a leapyear."
PLEASE DO NOT RESPOND TO THIS QUESTION IF YOU CANNOT ANSWER IT FULLY.
Explanation / Answer
#include<iostream>
#include<cstring>
#include<bits/stdc++.h>
using namespace std;
//class declaration
class date{
int month, day, year;
static const int date1[];
public:
void setdate(int m, int d, int y){
month = m;
day = d;
year = y;
}
date(){
month=1;
day=1;
year=1990;
}
bool leapyear(int);
bool end_of_month(int);
void nextday();
int count();
int day_left();
int days_month();
friend istream& operator >> (istream &,date &);
friend ostream& operator << (ostream &,date &);
};
void printopt();
int getopt();
const int date::date1[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main(){
date d1;
int year, month, day;
int opt, x;
while(1){
printopt();
opt = getopt();
cout << endl;
switch(opt){
case 0: exit(0);
case 1: cin >> d1;
cout << endl;
break;
case 2: d1.nextday();
cout << endl;
break;
case 3: cout << "Days since the begining of the year: " << d1.count() << endl << endl;
break;
case 4: cout << "Days left till the end of the year: " << 366-d1.count() << endl << endl;
break;
case 5: if(d1.leapyear(year) == 1){
cout << "This is a leap year";
}
else
cout << "This is not a leap year";
//cout << d1.leapyear(year);
cout << endl << endl;
break;
case 6: cout << "Number of days in the month " << d1.days_month() << endl << endl;
break;
case 7: cout << d1 << endl << endl;
break;
default:
break;
}
}
return 0;
}
//List the main user choices
void printopt(){
cout << "0: Exit the program." << endl;
cout << "1: Enter in a date." << endl;
cout << "2: Print the date for tomorrow." << endl;
cout << "3: # of the days since the begining of the year." << endl;
cout << "4: Days left till the end of the year." << endl;
cout << "5: Is the current year a leap year?" << endl;
cout << "6: Usual number of days for the current month." << endl;
cout << "7: Print the current entered date." << endl;
}
//User enters main choice
int getopt(){
int opt;
cout << "Pick option 0-7: ";
cin >> opt;
if (opt > 7){
cout << "Inccorect input" << endl;
return 0;
}
return opt;
}
//Case: 1
istream& operator >> (istream &i, date &i1){
int y, m, d;
cout << "Enter the date, hitting enter after month, date and year." << endl;
i >> m >> d >> y;
if(m <= 12 && (d >= 0 && d <= date[month]))
i1.setdate(m, d, y);
else
cout << "Enter date correctly";
}
//Case: 2
void date::nextday(){
if(month > 13)
cout << "invalid month" << endl;
else if(!end_of_month(day)){
day++;
cout << month << " : " << day << " : " << year << endl;
}
else if(month <= 12 && day < date1[month]){
day++;
cout << " Month: " << month << " Day: " << day << " Year: " << year << endl;
}
else if(day == date1[month] && month == 12){
month = 1;
day = 1;
year++;
cout << " Month: " <<month << " Day: " << day << " Year: " << year << endl;
}
else if(day == date1[month]){
month++;
day = 1;
cout << " Month: " << month << " Day: " << day << " Year: " << year << endl;
}
else{
year++;
month = 1;
day = 1;
cout << "Month: " << month << "Day: " << day << "Year: " << year << endl;
}
}
//Case: 3
int date::count(){
int days = 0;
for (int i = 1; i <= month-1; i++)
days += date1[i];
days += day;
if (leapyear(year) == false) days -= 1;
return days;
}
//Case: 4
int date::day_left(){
if (leapyear(year) == true)
return 366-count();
return 365-count();
}
//Case: 5 Leapyear Bool
bool date::end_of_month(int tday){
if(month == 2 && leapyear(year))
return tday = 29;
else
return tday;
}
bool date::leapyear(int year){
if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
return 1;
else
return 0;
}
//Case: 6 Private function that returnes the usual number of days in a given month (enter number 1-12)
int date::days_month(){
if (month == 2){
if (leapyear(year) == true) return 29;
return 28;
}
return date1[month];
}
//Case: 7
ostream& operator << (ostream &i, date &i1){
int y, m, d;
cout << "The date in Month Day, Year format is: ";
if (i1.month == 1)
cout << "January" << " " << i1.day << ", " << i1.year << endl << endl;
else if (i1.month == 2)
cout << "February " << i1.day << ", " << i1.year << endl << endl;
else if (i1.month == 3)
cout << "March " << i1.day << ", " << i1.year << endl << endl;
else if (i1.month == 4)
cout << "April " << i1.day << ", " << i1.year << endl << endl;
else if (i1.month == 5)
cout << "May " << i1.day << ", " << i1.year << endl << endl;
else if (i1.month == 6)
cout << "June " << i1.day << ", " << i1.year << endl << endl;
else if (i1.month == 7)
cout << "July " << i1.day << ", " << i1.year << endl << endl;
else if (i1.month == 8)
cout << "August " << i1.day << ", " << i1.year << endl << endl;
else if (i1.month == 9)
cout << "September " << i1.day << ", " << i1.year << endl << endl;
else if (i1.month == 10)
cout << "October " << i1.day << ", " << i1.year << endl << endl;
else if (i1.month == 11)
cout << "November " << i1.day << ", " << i1.year << endl << endl;
else
cout << "December " << i1.day << ", " << i1.year << endl << endl;
return i;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.