Implement a Days Calculator. The calculator will compute the number of days that
ID: 3906216 • Letter: I
Question
Implement a Days Calculator. The calculator will compute the number of days that elapsed between January 1, 1900 and a given date. You need to take intoaccount whether the entered year is a leap or not. A leap year is any year that is (divisible by 4 but not divisible by 100), or is (divisible by 400) The number of days in each month are as follows: January February 28 (29 for a leap 31 year) March April May June 31 30 August September October November December 30 31 31 30 31 30 31 Write a C++ program that does the following: 1- Displays a menu of two choices: a- Compute days since 1/1/1900 b Quit 2- Accepts a choice from the user 3- If choice enteredis not a or b prints an error message and asks for another choice. If choice is a: 4- a- Accepts one date. Dates should have the following format ddExplanation / Answer
Solution:
// C++ program two find number of days between two given dates
#include<iostream>
using namespace std;
// Creating struct to store date
struct Date
{
int d, m, y;
//d is day , m is month and y is year
};
//Default date from which we will calculate the date difference
Date dt1 = {1, 1, 1900};
// To store number of days in all months from January to Dec. except leap year
const int monthDays[12] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
// This function counts number of leap years before the
// given date
int countLeapYears(Date d)
{
int years = d.y;
// Check if the current year needs to be considered
// for the count of leap years or not
if (d.m <= 2)
years--;
// An year is a leap year if it is a multiple of 4,
// multiple of 400 and not a multiple of 100.
return years / 4 - years / 100 + years / 400;
}
// This function returns number of days between two given
// dates
int getDifference(Date dt1, Date dt2)
{
// COUNT TOTAL NUMBER OF DAYS BEFORE FIRST DATE 'dt1'
// initialize count using years and day
long int n1 = dt1.y*365 + dt1.d;
// Add days for months in given date
for (int i=0; i<dt1.m - 1; i++)
n1 += monthDays[i];
// Since every leap year is of 366 days,
// Add a day for every leap year
n1 += countLeapYears(dt1);
// SIMILARLY, COUNT TOTAL NUMBER OF DAYS BEFORE 'dt2'
long int n2 = dt2.y*365 + dt2.d;
for (int i=0; i<dt2.m - 1; i++)
n2 += monthDays[i];
n2 += countLeapYears(dt2);
// return difference between two counts
return (n2 - n1);
}
void takeDate(){
int second_date_month;
int second_date_days;
int second_date_year;
Date dt2;
while(true){
char choice;
cout<<"DAYS CALCULATOR"<<endl;
cout<<"---------------"<<endl;
cout<<"a- Compute days since 1/1/1900"<<endl;
cout<<"b- Quit"<<endl;
cout<<"Please enter your choice: ";
cin>>choice;//Taking choice from the user
switch(choice){
case 'a':
// Taking the second date and validating the same
do{
cout<<"Enter the date"<<endl;
cin >> second_date_days>> second_date_month >>second_date_year ;
dt2.d = second_date_days;
dt2.m = second_date_month;
dt2.y = second_date_year;
//Validating feb month if user types any date after 29
if(second_date_month == 2 && second_date_days>=29){
cout<<"Error: Invalid date, retry."<<endl;
}
// Validating year should be greater or equal to 1900
else if(second_date_year<1900){
cout<<"Error: Date must be greater than 1/1/1900, retry."<<endl;
continue;
}
// Validating date must in the range of 1 to 31
else if(second_date_days!=2 &&(second_date_days<0 || second_date_days>31)){
cout<<"Error: Invalid date, retry"<<endl;
continue;
}else{
break;
}
}while(true);
//If everything is good then showing the date difference
cout << "Days elapsed between the two dates: " << getDifference(dt1, dt2)<<" days."<<endl;
break;
case 'b': exit(0);
break;
default: cout<<"Error: Invalid menu choice, retry"<<endl;
break;
}
}
}
// Driver program
int main()
{
takeDate();
return 0;
}
Sample RUn:
DAYS CALCULATOR
---------------
a- Compute days since 1/1/1900
b- Quit
Please enter your choice: a
Enter the date
25 1 1976
Days elapsed between the two dates: 27782 days.
DAYS CALCULATOR
---------------
a- Compute days since 1/1/1900
b- Quit
Please enter your choice: a
Enter the date
30 2 2000
Error: Invalid date, retry.
Enter the date
30 1 1896
Error: Date must be greater than 1/1/1900, retry.
Enter the date
30 1 2006
Days elapsed between the two dates: 38745 days.
DAYS CALCULATOR
---------------
a- Compute days since 1/1/1900
b- Quit
Please enter your choice: d
Error: Invalid menu choice, retry
DAYS CALCULATOR
---------------
a- Compute days since 1/1/1900
b- Quit
Please enter your choice: b
Note:In question calculated days are wrong, you can google the days difference by providing 2 dates and then verify with this program. If you have any doubt before giving negative feedback do discuss in the comment. If you really liked the solution dont forget to give a big thumbs up.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.