Day of the year not computing.

Good Evening,

I have looked at this till I'm cross-eyed and then some. I just can't seem to find the reason it will not compute the correctly. No matter what date you put in, it will compute it as "1" Any clues of what I'm missing would be greatly appreciated

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
  int main()
{
    //Local constants
    int JAN = 31;
    int FEB = 28;
    int MAR = 31;
    int APR = 30;
    int MAY = 31;
    int JUN = 30;
    int JUL = 31;
    int AUG = 31;
    int SEP = 30;
    int OCT = 31;
    int NOV = 30;
    int DEC = 31;

    //Local variables
    int month;
    int day;
    int year;
    int dayYear;

    /********************Begin main Function Executables***********************/

    //Ask for the month, day and year
    cout << "Enter month (mm)  --->    ";
    cin >> month;
    
    cout << "Enter date (dd)  --->    ";
    cin >>  day; 
    
    cout << "Enter year (yyyy)  --->    ";
    cin >>  year;
    

    //Calculate the number of days
    	if	(month == 01)
		{dayYear = day;}
	
	else if	(month == 02)
    		{dayYear = JAN + day;}
    
    	else if	(month == 03)
    		{dayYear = JAN + FEB + day;}
    
		else if	(month == 04)
    		{dayYear = JAN + FEB + MAR + day;}
    		
		else if	(month == 05)
    		{dayYear = JAN + FEB + MAR + APR + day;}
    		
		else if	(month == 06)
    		{dayYear = JAN + FEB + MAR + APR + MAY + day;}
    		
		else if	(month == 07)
    		{dayYear = JAN + FEB + MAR + APR + MAY + JUN + day;}
    			
		else if	(month == 8)
    		{dayYear = JAN + FEB + MAR + APR + MAY + JUN + JUL + day;}
    		
		else if	(month == 9)
    		{dayYear = JAN + FEB + MAR + APR + MAY + JUN + JUL + AUG + day;}
    		
		else if	(month == 10)
    		{dayYear = JAN + FEB + MAR + APR + MAY + JUN + JUL + AUG + SEP + day;}
    		
		else if	(month == 11)
    		{dayYear = JAN + FEB + MAR + APR + MAY + JUN + JUL + AUG + SEP + OCT + day;}
			 	
		else if	(month == 12);
    		{dayYear = JAN + FEB + MAR + APR + MAY + JUN + JUL + AUG + SEP + OCT + NOV + day;}
    
    // Calculate if leap year
    	if (year % 4 == 0 && year % 100 == 0 && month > 2) 
             {dayYear = dayYear + 1;  }

	else (year % 400 == 0 && month > 2); 
             {dayYear = dayYear = 1;}
    
    //Display the day of year
    cout << "The day of the year is    " << dayYear << endl;

	
	//Hold execution on screen
    system ("pause");

    //Indicate to OS successful termination of program
    return 0;

}   //End main 
If I change the indentation of part of your code, let us see if it makes what is wrong clearer:

1
2
3
4
5
6
7
8
9
10
11
    // Calculate if leap year
    if (year % 4 == 0 && year % 100 == 0 && month > 2) 
    {
        dayYear = dayYear + 1;  
    }
    else 
        (year % 400 == 0 && month > 2);

    {
        dayYear = dayYear = 1;
    }


Notice that your else clause is a single expression that does not include an assignment statement. It is just the expression "(year % 400 == 0 && month > 2)". The else clause does not have a condition. Did you intend to have an if ... else if ...?

Your if ... else ... statement is followed by a block which includes a single statement that assigns 1 to dayYear twice. That block is not part of the else clause. The behavior may be undefined because you are setting the same variable twice between sequence points. Did you intend to add 1 to dayYear and store it in dayYear? If so, that is not what is written.
Last edited on
Lines 77 and 78:
1
2
else (year % 400 == 0 && month > 2); 
             {dayYear = dayYear = 1;}


The semi-colon at the end of line of line 77 cuts off the else statement. Therefore, line 78 is outside of the if/else block, so your variable is always assigned the value 1.

Four things to note as well:
1) You should be using else if, not else, because you have a condition.
e.g.
1
2
3
4
   if(this){}
   else if(that){}
   else if(another that){}
   else{}


2) I think you meant dayYear = dayYear + 1 on line 78.

3) You can save a lot of time if you use an array to hold all the constant data, then use a for loop to add the constant values with the iteration limited by the month value. Then, you could have a single if statement before the for loop to check if the month value is valid.
Note that I provide no sample code. I hope to see you figure it out yourself.

4) You can get rid on lines 77 and 78 by changing the condition of the previous if statement. The year is NOT a leap year if it is divisble by 100, so you could just have if(year%4 == 0 && year%100 != 0 && month > 2)
What does line 78 do?

The else statement cannot have a condition, plus realise that a year is a leap year or it isn't.

Remove the semicolon from line 70.

Also, consider putting the month data into an array & using a for loop to sum the values up to a given month. That way you can do the calculation in about 3 lines instead of 35. If you have a lot of repetition or tedious code, then there is probably a better way.

The leading zero's you have on the month values means that you have specified them in octal (base 8).

Line 75 can be written simply as dayYear++; there are also the +=, -=, *=, /= operators (plus more) which you can use use if saying adding a number bigger than 1 for example:

1
2
MyNum += 5;  // adds 5 to MyNum 
MyNum *= 5; // multiplies MyNum by 5 


Your leap year calculation isn't right - every 100th year isn't, but every 400th year is. This can be written as 1 statement.

Hope all goes well. :)
Topic archived. No new replies allowed.