### 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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990`` `````` 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:

 ``1234567891011`` `````` // 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:
 ``12`` ``````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.
 ``1234`` `````` 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:

 ``12`` ``````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.