Write a program to accept any two dates in the form of month, day, and year (8 2
ID: 3545089 • Letter: W
Question
Write a program to accept any two dates in the form of month, day, and year (8 23 2000), separated by spaces, and calculate the total number of days that has elapsed between the two dates, inclusive of the beginning and ending days. Remember that leap year is a year divisible by 4, except for centennial years, which must be divisible by 400, such as 1600 or 2000 (1800 is not a leap year). You can test your program with any typed in dates but it must finally run with the data shown below. System output on the screen is acceptable. DATA: ANSWERS 7 4 1776 1 1 1987 (76882) 2 1 1983 3 15 1984 (409) 7 7 1983 11 4 1983 (121) 7 3 1983 7 20 1983 (18) 12 25 1990 12 31 1992 (738) . Consider using the long data type for day. Counter Program must continue running until all the data is processed - use a loop 1 to 6 or a while (month != -1) or while (!fin.eof()) - choose your own end of data technique. Use if statements, switch statements, and loops to solve this problem. Do not use the Gregorian date method! Read the data from a defined data file - do not type in the data when the program runs. Use only c++ language in your program.
Explanation / Answer
please rate - tjhanks
any questions ask-file input
#include <iostream>
#include <fstream>
using namespace std;
int leap(int);
int main()
{int daysinmonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int month,day,year,days;
int month2,day2,year2;
bool valid;
char filename[30];
cout<<"what is the name of the file you are using? ";
cin>>filename;
ifstream input;
input.open(filename); //open file
if(input.fail()) //is it ok?
{ cout<<"file did not open please check it ";
system("pause");
return 1;
}
input>>month;
while(input)
{input>>day>>year>>month2>>day2>>year2;
cout<<" The number of days between "<<month<<"/"<<day<<"/"<<year<<
" and "<<month2<<"/"<<day2<<"/"<<year2<<" is: ";
if(month==month2&&year==year2) //same month?
days=day2-day+1;
else
{ //get days till end of month
days=daysinmonth[month-1]-day+1; //includes ending day
if(month==2)
{days=days+leap(year); //if feb and leapyear add another day
}
do //then add to that number of days for full months
{month++;
if(month==13)
{month=1;
year++;
}
days+=daysinmonth[month-1];
{ if(month==2)
{
days=days+leap(year); //if feb and leapyear add another day
}
}
}while(month!=month2||year!=year2);
days=days-daysinmonth[month-1]; //add days for last month
days+=day2; //and day up to
}
cout<<days<<" days"<<endl;
input>>month;
}
input.close();
system("pause");
}
int leap(int year)
{int leapcode=0; //return 1 if leapyear
if(year%4==0) //otherwise return 0
{if(year%100!=0)
leapcode=1;
else
if(year%400==0)
leapcode=1;
}
return leapcode;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.