I cant figure out why this message is popping up. This is a program that is calculates if it is a leap year.

#include <iostream>
using namespace std;

int main()
{
    double year, mod4, mod100, mod400;
    bool leapYear;
    cout << "Enter a year from 1582 on.\n";
    cin >> year;
    
    /* 0=False  1=True */
    mod4 = year%4; 
    mod100 = year%100;
    mod400 = year%400;
    
    /* If year is divisible by 4 check to see if it is divisible by 100.
       If divisible by 100 check to see if it is divisible by 400.
       If not it is not a leap year. */
    if(mod4=0 && mod100 != 0) leapYear = true;
    else if(mod100=0 && mod400=0) leapYear = true;
    else leapYear = false;       
                  
 return 0;  
}

Recommended Answers

All 5 Replies

Use integers.

why is year a double??? There are no decimal places in a year unless you want 1.5 to be a years and 6 months.

I changed the code to

#include <iostream>
using namespace std;

int main()
{
    int year;
    double mod4, mod100, mod400;
    bool leapYear;
    cout << "Enter a year from 1582 on.\n";
    cin >> year;
    
    /* 0=False  1=True */
    mod4 = year%4; 
    mod100 = year%100;
    mod400 = year%400;
    
    /* If year is divisible by 4 check to see if it is divisible by 100.
       If divisible by 100 check to see if it is divisible by 400.
       If not it is not a leap year. */
    if(mod4=0 && mod100 != 0) leapYear = true;
    else if(mod100=0 && mod400=0) leapYear = true;
    else leapYear = false;       
                  
 return 0;  
}

the compiler says the error is on line 21

else if(mod100=0 && mod400=0) leapYear = true;

the error is non-lvalue in assignment

Wow. I figured out the problem.
Instead of

else if(mod100 = 0 && mod400 = 0) leapYear = true;

I needed

else if(mod100 == 0 && mod400 == 0) leapYear = true;

I was trying to assign a new value instead of compare it. Stupid mistake!

Be a part of the DaniWeb community

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