C++ Day of the Year Calculator whats wrong with my code?!

I’m working on a school project where I have to calculate the day of the year using any given date. Currently I’m trying to calculate whether or not a year is a leap year, but something is wrong with my code:

#include <iostream>

using namespace std;

bool isLeapYear(int);
int main() {
    // Write your main here
    int mo, day, yr;
    int year = 365;
    
    cout << "Enter the date: " << endl;
    cin >> mo >> day >> yr;
    cout << mo << endl;
    cout << day << endl;
    cout << yr << endl;
    
    isLeapYear(yr);
    
    if (isLeapYear > 0)
        year++;
    
    cout << year;
    return 0;
}

bool isLeapYear(int yr)
{
    if (return (yr % 4 == 0 && yr % 100 != 0) || yr % 400 == 0;)
        return true;
    else
        return false;
}

I would appreciate any insight! For some reason all years are returning as leap years?! It’s due 11-9-19 :fearful:

What do you mean by wrong? Finding the source of a problem usually comes from examining the problem itself. Personally I wonder where exactly you’re getting stuck while investigating it, what information are you missing to find and fix it?

Here are the error messages produced by two different compilers. Ideally you’d look at this when compiling yourself so that you are using all the right flags and so on (and make sure to enable warnings, likely with a flag something like -Wall)

$ clang a.cpp 
a.cpp:19:20: error: ordered comparison between pointer and zero ('bool (*)(int)' and 'int')
    if (isLeapYear > 0)
        ~~~~~~~~~~ ^ ~
a.cpp:28:9: error: expected expression
    if (return (yr % 4 == 0 && yr % 100 != 0) || yr % 400 == 0;)
        ^
2 errors generated.
$ g++ a.cpp
a.cpp: In function ‘bool isLeapYear(int)’:
a.cpp:28:9: warning: init-statement in selection statements only available with ‘-std=c++17’ or ‘-std=gnu++17’
   28 |     if (return (yr % 4 == 0 && yr % 100 != 0) || yr % 400 == 0;)
      |         ^~~~~~
a.cpp:28:9: error: expected primary-expression before ‘return’
a.cpp:28:64: error: expected primary-expression before ‘)’ token
   28 |     if (return (yr % 4 == 0 && yr % 100 != 0) || yr % 400 == 0;)
      |                                                                ^
a.cpp:32:1: warning: control reaches end of non-void function [-Wreturn-type]
   32 | }
      | ^

so I don’t think the function isLeapYear is returning right? I keep entering 1-1-1800 as a date and it keeps returning as a leap year even though it isn’t? I think there’s something wrong with my logic?

It’s not doing a whole lot of anything if it doesn’t compile.
Maybe you’re either showing different code than you’re using, or maybe you’re running an old executable

For observing your logic I suggest printing out what’s being done. You will of course have to get it to run at all first.
Another thing you can do is to run LESS code - comment some out, see if what remains is okay, slowly add things back. It’s easier to find problems when you’re looking at less code.

ok so here is my code. I think I’ve commented out everything that doesn’t involve the leap year. and i think ive solved some of the problems in the isLeapYear function.

#include <iostream>

using namespace std;

bool isLeapYear(int);
int main() {
    // Write your main here
    int mo, day, yr;
    /*int year = 365;*/
    
    cout << "Enter the date: " << endl;
    cin >> mo >> day >> yr;
    /*cout << mo << endl;
    cout << day << endl;
    cout << yr << endl;*/
    
    isLeapYear(yr);
    cout << isLeapYear;
    
    /*if (isLeapYear > 0)
        year++;
    
    cout << year;*/
    return 0;
}

bool isLeapYear(int yr)
{
    if ((yr % 4 == 0 && yr % 100 != 0) || yr % 400 == 0)
        return true;
    else
        return false;
}

ok so I’m noticing in

bool isLeapYear(int yr)
{
    if ((yr % 4 == 0 && yr % 100 != 0) || yr % 400 == 0)
        return true;
    else
        return false;
}

specifically in the if statement that 1800 / 4 leaves no remainder
1800 / 100 also leaves no remainder, so the first part evaluates to false.
but 1800 /400 also leaves no remainder so the overall if statement evaluates to true.
to fix this i changed the || operator to &&
but for some reason even this is evaluating to true?
here’s my code:

#include <iostream>

using namespace std;

bool isLeapYear(int);
int main() {
    // Write your main here
    int mo, day, yr;
    /*int year = 365;*/
    
    cout << "Enter the date: " << endl;
    cin >> mo >> day >> yr;
    /*cout << mo << endl;
    cout << day << endl;
    cout << yr << endl;*/
    
    isLeapYear(yr);
    cout << isLeapYear;
    
    /*if (isLeapYear > 0)
        year++;
    
    cout << year;*/
    return 0;
}

bool isLeapYear(int yr)
{
    if ((yr % 4 == 0 && yr % 100 != 0) && yr % 400 == 0)
        return true;
    else
        return false;
}

What you can do for that is create a super minimal program on the side that does absolutely nothing other than those operations one at a time printing out intermediate results.

However.

Your code does not compile without warnings. No point to go looking for problems when there are already ones pointed out to you by the compiler.

lmao I finally figured it out. took me long enough. so the problem was in the code the compiler already warned me about. i think it was right here: cout << isLeapYear; i think i was forgetting to pass the parameter? so the correct code would look like: cout << isLeapYear(yr);

so previously the code was evaluating to true because my if statement here:

if (isLeapYear > 0)
        year++;
    
    cout << year;

didn’t have a parameter, so the correct code would be:

if (isLeapYear(yr) > 0)
        year++;
    
    cout << year;

Here’s my overall code after clean up:

#include <iostream>

using namespace std;

bool isLeapYear(int);
int main() {
    // Write your main here
    int mo, day, yr;
    int year = 365;
    
    cout << "Enter the date: " << endl;
    cin >> mo >> day >> yr;
    cout << mo << endl;
    cout << day << endl;
    cout << yr << endl;
    
    isLeapYear(yr);
    
    if (isLeapYear(yr) > 0)
        year++;
    
    cout << year;
    return 0;
}

bool isLeapYear(int yr)
{
    if ((yr % 4 == 0 && yr % 100 != 0) && yr % 400 == 0)
        return true;
    else
        return false;
}

Your isLeapYear is a pointer to a location in memory where your function exists.
Converting that pointer to bool gives you true.
This does not make sense, that is why the compiler tells you so.
cpp does what you tell it.

also thank you so much @ionatan you were pointing me toward the problem the whole time :joy:

thank you so much, it makes a lot more sense now