The first thing to notice is that any year which is divisible by 400 is automatically divisible by 100. So the second condition will never be executed:
if (year % 100)
isleap = true;
else if ((year % 400))
isleap = true;
The second thing to notice is that in C - and by extension in C++ - a zero value is false, and any non-zero value is true. So for
x % 4
there will be 3 true values, and one false value.
In addition, you never set
isleap
to anything other than true anyway ...
Seriously, if that code works at all I'd be surprised - I suspect you tested a single value (or very select values) to see if it worked, and the whole thing is miles out.
Two things I'd strongly recommend:
1) Don't use "global variables" to pass and return values: return a
bool
value directly from the function instead:
bool IsLeapYear(int year)
{
...
}
2) Learn to use the debugger - it will save you huge amounts of time when your code doesn't do what you expected it to!