-1
bool leap = false;
int leapd = 28;

if (year % 100 == 0) {
    if (year % 400 == 0) {
        leap = true;
    }
    else { leap = false; }
}

else if (year % 4 == 0) {
    leap = true;
}
else { leap = false; }
if (leap = true) {
    leapd++;
}

This program decides if the year entered is a leap year or not. I'm not sure why, but the program will always set leap to true in the end.

If the year is divisible by 100, than it is a leap year if it also is divisible by 400.

If it is not divisible by 100, it is only a leap year if you can divide the year by 4.

pushkin
  • 9,575
  • 15
  • 51
  • 95
  • 1
    Most compilers have a warning for exactly the mistake you made. You might want to make sure you have a good set of default warnings enabled. – aschepler Mar 12 '18 at 02:44

2 Answers2

2

It should be if (leap == true) for your last if statement. If you fix that, it should work. = is the assignment variable. So in your last if statement, instead of comparing leap and true using == you are assigning leap to true using =.

So your final if statement should actually be:

if (leap == true)
{
    leapd++;
}
  • @aschepler. Yes that's true. But I think what I have stated in my answer will be easier for the OP as it looks like he's a beginner. –  Mar 12 '18 at 02:44
  • `if (leap)` is not only the preferred way (if I’m not mistaken), but is also sure to prevent the same mistake from being made again. – Ole V.V. May 01 '18 at 11:19
  • @OleV.V. I'm a python guy so I believe in explicit rather than implicit ;) –  May 02 '18 at 23:52
  • Funny, @BOI. I’m mostly a Java guy these days and find `if (leap)`, or better, `if (isLeapYear)` the most explicit. `== true` is just redundant. I certainly never saw it when I coded C++ in the 1990’s either. Just teasing: do you then find `if ((leap == true) == true)` still more explicit? ;-) – Ole V.V. May 03 '18 at 03:53
0

The last loop should look like

if (leap == true)
{
    leapd++;
}

The way you currently have it will set leap to True and then evaluate the if. Which will always be True. The condition check should be ==.

JahKnows
  • 2,618
  • 3
  • 22
  • 37