Write a C++ program that calculates the number of days between two dates within
ID: 673663 • Letter: W
Question
Write a C++ program that calculates the number of days between two dates within the same year. Each date will consist of a day and month. This program will use two functions:
1- A Boolean function to check if a date is a valid date.
bool valid_date(int day, int month);
The function valid_date will take as input the day and the month and will return true if both of them are valid values and will return false if they are not.
You need to validate for the following:
The day is a valid value greater than 0 and is less than 32.
The month is greater than 0 and less than 13
The day is valid for the associated month. Meaning, if the month is 2 which is February then the user can not enter 30 for the day. Assume that February is 28 days.
2- An integer function that will return the number of days between two dates.
int calc_days(int day1, int month1, int day2, int month2);
The function calc_days will take as input four integer values and will return the number of days between the two dates.
Always assume that month2 is greater than month1.
In the main program,
1- Ask the user to enter the first day and month. Call the function valid_date to validate the input. Continue to loop as long as the input is not valid. Looping is performed in the main program and not in the function. The user will be required to enter both day and month again if either one is wrong.
2- Ask the user to enter the second day and month. Call the function valid_date to validate the input. Continue to loop as long as the input is not valid.
3- Call the function calc-days to calculate the number of days between the two dates.
4- Finally in the main program print to screen the two dates and the number of days between these two dates.
Use several test cases to check all types of input
Explanation / Answer
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
bool valid_date(int day,int month){
bool flag=false;
if(day<=0||day>31||month<1||month>12){
flag=false;
}else{
switch(month){
case 1:
if(day<=31)flag=true;
break;
case 2:
if(day<=28)flag=true;
break;
case 3:
if(day<=31)flag=true;
break;
case 4:
if(day<=30)flag=true;
break;
case 5:
if(day<=31)flag=true;
break;
case 6:
if(day<=30)flag=true;
break;
case 7:
if(day<=31)flag=true;
break;
case 8:
if(day<=31)flag=true;
break;
case 9:
if(day<=30)flag=true;
break;
case 10:
if(day<=31)flag=true;
break;
case 11:
if(day<=30)flag=true;
break;
case 12:
if(day<=31)flag=true;
break;
default:
break;
}
}
return flag;
}
int calc_days(int day1, int month1, int day2, int month2){
int n=0;
if(month1==month2){
n=day2-day1;
}else{
int i=0;
for(i=month1;i<=month2;i++){
switch(i){
case 1:
if(i==month1){
n=31-day1;
}else if(i==month2){
n=n+day2;
}
break;
case 2:
if(i==month1){
n=28-day1;
}if(i==month2){
n=n+day2;
}else{
n=n+28;
}
break;
case 3:
if(i==month1){
n=31-day1;
}if(i==month2){
n=n+day2;
}else{
n=n+31;
}
break;
case 4:
if(i==month1){
n=30-day1;
}if(i==month2){
n=n+day2;
}else{
n=n+30;
}
break;
case 5:
if(i==month1){
n=31-day1;
}
break;
case 6:
if(i==month1){
n=30-day1;
}if(i==month2){
n=n+day2;
}else{
n=n+30;
}
break;
case 7:
if(i==month1){
n=31-day1;
}if(i==month2){
n=n+day2;
}else{
n=n+31;
}
break;
case 8:
if(i==month1){
n=31-day1;
}if(i==month2){
n=n+day2;
}else{
n=n+31;
}
break;
case 9:
if(i==month1){
n=30-day1;
}if(i==month2){
n=n+day2;
}else{
n=n+30;
}
break;
case 10:
if(i==month1){
n=31-day1;
}if(i==month2){
n=n+day2;
}else{
n=n+31;
}
break;
case 11:
if(i==month1){
n=30-day1;
}if(i==month2){
n=n+day2;
}else{
n=n+30;
}
break;
case 12:
if(i==month1){
n=31-day1;
}if(i==month2){
n=n+day2;
}else{
n=n+31;
}
break;
}
}
return n;
}
}
int main() {
int day1,month1,day2,month2;
do{
cout<<"Enter date1: ";
cin>>day1;
cout<<"Enter month1: ";
cin>>month1;
}while(!valid_date(day1,month1));
do{
cout<<"Enter date2: ";
cin>>day2;
cout<<"Enter month2: ";
cin>>month2;
}while(!valid_date(day2,month2));
cout<<"Difference between dates: "<<calc_days(day1,month1,day2,month2)<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.