<p>Write and test a program to calculate the day number (from 1 to 365 in a regu
ID: 3632592 • Letter: #
Question
<p>Write and test a program to calculate the day number (from 1 to 365 in a regular year, or from 1 to 366 in a leap year), when a date is input.</p><p>Input file: month (1 to 12 allowed), day of month (1 to 31 allowed) and year (4-digit number), in month/day/year form. The number of dates in the file is unknown. Sample input:</p>
<p>5/14/1950</p>
<p>11/3/2000</p>
<p>1/7/3026</p>
<p>Output file: For each month/day/year input:</p>
<p>month/day/year is day number ________(day number).</p>
<p>The year _______(year) is (or is not) a leap year.</p>
<p>Processing requirements:<br />Do input testing, and report the problem if illegal input occurs. Continue processing the next input.</p>
<p>Use a switch statement testing t = (month-1), with case ordered from 11 to 1, to add the number of days in month t to the day number. Do not use breaks between cases, because if you are i month 7, for example, you need to add the days in all the months 6, 5, 4, 3, 2, and 1 to the day of the month in month 7.</p>
<p>Follow Program Guidelines. Include a header and good comments explaining what the program is doing.</p>
<p>Do at least the following runs, and be sure the program works for these cases:<br />a January date<br />a February date in a leap year<br />a June date in a leap year<br />an October date in a non-leap-year<br />cases with bad input to demonstrate your input checking works</p>
<p>Notes:<br />Leap year occurs when the year number is divisible by 4, except that years divisible by 100 are not leap years unless they are also divisible by 400. (Year 1900 was not a leap year, but year 200 was a leap year.)<br /><br />Turn in: Your source file, and a copy of the output file.</p>
Explanation / Answer
please rate - thanks
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void getdate(int&,int&,int&,string);
int todoy(int,int,int);
int leap(int);
int main()
{
int month,day,doy,year,d;
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
ifstream in;
ofstream out;
bool error;
char filename[80];
string input;
cout<<"enter input file name: ";
cin>>filename;
in.open(filename);
if(in.fail())
{cout<<"input file did not open please check it ";
system("pause");
return 1;
}
cout<<"enter output file name: ";
cin>>filename;
out.open(filename);
getline(in,input);
while(in)
{getdate(month,day,year,input);
error=false;
out<<input<<" ";
if(month<1||month>12)
{out<<"invalid month ";
error=true;
}
if(month!=2)
if(day<1||day>days[month-1])
{ out<<"Invalid day ";
error=true;
}
if(month==2)
{d=days[1]+leap(year);
if(day<1||day>d)
{ out<<"Invalid day ";
error=true;
}
}
if(!error)
{doy=todoy(month,day,year);
out<<"is day number "<<doy<<endl;
if(leap(year)==0)
out<<"The year "<<year<<" is not a leap year";
else
out<<"The year "<<year<<" is a leap year";
}
out<<endl;
getline(in,input);
}
return 0;
}
void getdate(int& m,int& d,int& y,string in)
{int i,j;
i=in.find('/',0);
m=atoi(in.substr(0,i).c_str());
i++;
j=in.find('/',i);
d=atoi(in.substr(i,j-i).c_str());
y=atoi(in.substr(j+1,in.length()-j).c_str());
}
int todoy(int month,int day,int year) //change date to day of year
{int daysinmonth;
int days=0,i;
for (i=1;i<month;i++) //go up to that month add how many days in the month
{ switch(i)
{
case 9:
case 4:
case 6:
case 11:daysinmonth=30;
break;
case 2: daysinmonth=28 +leap(year);
break;
default:daysinmonth=31;
}
days+=daysinmonth; //and count how many days passed
}
days+=day; //add to the day wanted
return days;
}
int leap(int year)
{int leapcode=0;
if(year%4==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.