OKAY THIS PROGRAM IS SUPPOSED TO INPUT THE DATE AND YEAR FROM A FILE
THEN OUTPUT IF ITS A LEAP YEAR, AND WHAT DAY OUT OF 365 DAYS IN THE YEAR IT IS
I WOULD APPRECIATE IT IF I COULD GET AN EXTRA SET OF EYES ON IT IM HAVING PROBLEMS
#include<iostream>
#include<fstream>
#include<iomanip>
usingnamespace std;
int main()
{
int month,day, days, year;
ifstream infile;
ofstream outfile;
infile.open("e:\\input.txt");
outfile.open("e:\\output.txt");
infile>>month>>day>>year;
if (month<1 || month>12)
{
outfile<<"invalid...please try again!"<<endl;
}
elseif(year%4==0 && !year% 100==0||year%400==0)
{
switch (month)
{
case 12:
days=days+30;
case 11:
days=days+31;
case 10:
days=days+30;
case 9:
days=days+31;
case 8:
days=days+31;
case 7:
days=days+30;
case 6:
days=days+31;
case 5:
days=days+30;
case 4:
days=days+31;
case 3:
days=days+29;
case 2:
days=days+31;
case 1:
days=days;
default:
outfile<<"invalid value"<<endl;
}
}
else
{
switch (month)
{
case 12:
days=days+30;
case 11:
days=days+31;
case 10:
days=days+30;
case 9:
days=days+31;
case 8:
days=days+31;
case 7:
days=days+30;
case 6:
days=days+31;
case 5:
days=days+30;
case 4:
days=days+31;
case 3:
days=days+28;
case 2:
days=days+31;
case 1:
days=day;
default:
outfile<<"invalid value"<<endl;
}
}
if(year%4==0 && !year% 100==0||year%400==0)
{
outfile<<"The year "<<year<<"is a leap year!"<<endl;
}
else
{
outfile<<"the year "<<year<<"is not a leap year."<<endl;
}
outfile<<month<<"/"<<day<<"/"<<year<<"is day number "<<days<<"."<<endl;
infile.close();
outfile.close();
return 0;
}
That's true- if you want the cases to "fall through" and execute all the following code; which I now see may be what you intended all along. My mistake :-)
OK, have some more shot-in-the-dark suggestions:
-Initialize days to some value before the switch statement (0?)
-add more parenthesis to your ifs to be absolutely sure that the order of operations is the way you want it. Something like this:
If you look at your code, you are not adding the right days. Case 2: is for February and you have it adding 31 days when it should be 28 or 29 depending on if it is a leap year. And your also missing the month of December because of it.
1 2 3 4 5 6
case 3:
days=days+28;
case 2:
days=days+31;
case 1:
days=day;
Your switch statements aren't working because if the date contains a month number, what happens if the day is in the middle of that month? You are always adding the maximum number of days, as if the month has already passed. What you want to do is add the number of days that have passed only in the previous months. THEN add the day portion of the date to that number.
What you can do is make constant values for your days;
int days = 0; // it was declared, but not initialized
switch (month - 1) // subtract 1 from current month, then add every month complete
{
case 11:
days=days+30;
case 10:
days=days+31;
case 9:
days=days+30;
case 8:
days=days+31;
case 7:
days=days+31;
case 6:
days=days+30;
case 5:
days=days+31;
case 4:
days=days+30;
case 3:
days=days+31;
case 2:
days=days+28;
case 1:
days=days+31;
case 0:
days=days + day; // add the completed days for current month
default:
outfile<<"invalid value"<<endl;
}
For the leap year, you will need to copy this whole switch statement and just change case 2 to:
1 2
case 2:
days = days + 29;
You can use just 1 switch statement for both leap years and non leap years, but that is something to discuss later if you want.