Can anyone tell me whay the following funtion that should be returning true if the year is leap year and false if it is not, is givign me unexpected results? when I entered 1985 I got true? when I entered 2012 I got false. When I entered 2000 I got true, it was actually right on that one. I'M building a program that will validate if a date is valid or not based on the number of days in each month, I need leap year to determin the max days in FEB, etc... I frist wrote the program in bash and it worked just fine. Now that I've converted the formula over to C++ it's not working out.

``````bool is_leap_year( int year )
{
bool result = false;
int local_year = year;

if ( 0 == (( local_year % 4 ) && 0 != ( local_year % 100 )) || 0 != ( local_year % 400 ) )
{
result = true;
}

return result;
}
/*END*/
``````

``````bool is_leap_year( int year )
{
if ( year % 4 == 0 ) return true;
return false;
}
``````

I think your formula is wrong, here's my take.

``````bool is_leap_year( int year )
{

if ((year % 4)) {
return false;
}
if ((year % 100)) {
return true;
}
if ((year % 400)) {
return false;
}
return true;
}

int main()
{
for (int …``````

I still have time until 2100 when my formula will blow up. And, definitely, I am not that old to know how was in 1900. ;)

No offense, but that's the most asinine thing I've read all day.

But I think it happens even to more experienced people, don't you agree?

Yup, though more experienced people tend to welcome others pointing out such mistakes.

On the other hand, interesting enough that you felt like giving me a lesson instead of simply posting the correct way.

## All 13 Replies

``````bool is_leap_year( int year )
{
if ( year % 4 == 0 ) return true;
return false;
}
``````

I think your formula is wrong, here's my take.

``````bool is_leap_year( int year )
{

if ((year % 4)) {
return false;
}
if ((year % 100)) {
return true;
}
if ((year % 400)) {
return false;
}
return true;
}

int main()
{
for (int i= 0; i < 1000; i++) {

cout << is_leap_year(2000 + i) << endl;
}

return 0;
}
``````

Now that I've converted the formula over to C++ it's not working out.

It's the wrong formula. This is correct:

``````bool is_leap_year(int year)
{
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
``````

if ( year % 4 == 0 ) return true;

You might want to read up on leap years.

You might want to read up on leap years.

I still have time until 2100 when my formula will blow up. And, definitely, I am not that old to know how was in 1900. ;)

I still have time until 2100 when my formula will blow up. And, definitely, I am not that old to know how was in 1900. ;)

No offense, but that's the most asinine thing I've read all day.

No offense, but that's the most asinine thing I've read all day.

Oh! Big words for an admin, don't you think? Relax, it was only a joke which wanted to say that I am not working in my calendars with dates so far as year 1900 and I don't expect any of my applications to live for 87 years from now.

Big words for an admin, don't you think?

As an admin I have to show that I'm a step above the average semiliterate web denizen. I'm sorry if I made you look up any words to figure out what I was saying. ;)

Relax, it was only a joke which wanted to say that I am not working in my calendars with dates so far as year 1900 and I don't expect any of my applications to live for 87 years from now.

I figured that was the case, but it's irrelevant what you need the formula for, and the joke wasn't obvious from your code. If you're posting code as help for someone else, you can't assume that their needs match your needs, especially when your needs are a teeny tiny subset of possible applications.

It's no different than when someone posts code that only compiles under Turbo C++ with the implicit assumption that everyone else uses that compiler. Someone will call you on it.

I figured that was the case

Did you? Strangely enough but you didn't show it.

If you're posting code as help for someone else, you can't assume that their needs match your needs, especially when your needs are a teeny tiny subset of possible applications.

You are right, I cannot assume that. My bad I answered rushly. I apologize for that. But I think it happens even to more experienced people, don't you agree? On the other hand, interesting enough that you felt like giving me a lesson instead of simply posting the correct way. I wonder if it is related to my answers to you by now.

It's no different than when someone posts code that only compiles under Turbo C++ with the implicit assumption that everyone else uses that compiler. Someone will call you on it.

Hmm... I am wondering what you refer to here? That's because I have no idea what you meant there.

Nevertheless, this conversation gets way out of the subject, don't you think?

But I think it happens even to more experienced people, don't you agree?

Yup, though more experienced people tend to welcome others pointing out such mistakes.

On the other hand, interesting enough that you felt like giving me a lesson instead of simply posting the correct way.

I didn't give you a lesson, I pointed out that your code suggested a distinct lack in your understanding of leap years. Thus I recommended you do some more research. And you'll notice that I did post the correct way, though it was in response to the OP rather than you.

I wonder if it is related to my answers to you by now.

I won't deny that the likelihood of someone responding in an entertaining way affects how I choose to phrase my posts. :D

Hmm... I am wondering what you refer to here? That's because I have no idea what you meant there.

I meant that you made an unwarranted assumption (some might even call it stupid) and gave a common example of another assumption that's equally annoying.

Nevertheless, this conversation gets way out of the subject, don't you think?

The question has been answered. We won't know if the OP needs more help until/if he replies.

Well, I tried the following and it approved 1985 as a leap year. So id did not work.

`if ( local_year % 4 == 0 && ( local_year % 100 != 0 || local_year % 400 == 0 ) );`

I suspect that semicolon at the end has something to do with it not doing what you want:

``````#include <iostream>

int main()
{
int year = 1985;

if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
std::cout << "Leap year\n";
else
std::cout << "Not a leap year\n";
}
``````

I gave you a complete function, there was no need to change it. Especially since by changing it you apparently broke it.

I think he copied the return statement and forgot to remove the semicolon after changing it to an if. Not sure why he would do that but hey.

Thanks guys. It's working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.