I NEED HELP IM NEW AND A BEGINNER

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace 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;
     }
     
   else if(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;
 
    
}
Check your caps lock, friend. I think it's on by mistake.

If I'm not mistaken, switch()s require a "break;" in between each case. Something like this.
1
2
3
4
5
case 12:
days=days+30;break;
case 11:
days=days+31;break;
//etc... 

you may want to put break on its own line, or not. Whatever works.
thanks but that didnt work
i still got the same output
breaks are not something that has to be there
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((((year%4)==0) && !((year% 100)==0))||((year%400)==0))
>>if((((year%4)==0) && !((year% 100)==0))||((year%400)==0))

Hmmm, confusing yet ?!?!??

Try this simple (on top adding breaks in your case statement) replacement

else if (year%4==0 && !(year%100==0 || year%400==0) )

Check it out. Good luck :)




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;
Last edited on
never mind what i had here.
Last edited on
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;

1
2
3
4
const int DAYS_THRU_JAN=31;
const int DAYS_THRU_FEB=DAYS_THRU_JAN+28;
const int DAYS_THRU_MAR=DAYS_THRU_FEB+31;
const int DAYS_THRU_APR=DAYS_THRU_MAR+30;

...etc

Then in your switch statment, use the appropriate constant for calculating your days into that month:

1
2
3
switch (month) {
  case 12: days=DAYS_THRU_NOV + day; break;
  case 11: days=DAYS_THRU_OCT + day; break;

...etc

Remember that you need to add an extra 1 to days on leap-year, but only if the month is after the end of February!
Wow, that is a lot of coding, the easier way to do it is -
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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.
Last edited on
Topic archived. No new replies allowed.