-3

Okay, so, ignoring my lazy coding (this is just to get the program to work, I'll clean it up after I get it working). I've set up a couple of if statements that will throw exceptions if I don't get the input I'd like.

#include<string>
#include<iostream>
using namespace std;

int main()
{

    bool flag = false;
    int month, day, year;
    void header();

    class monthClassException
    {
    public:
        monthClassException()
        {
            message = "Invalid Month"; 
        }
        monthClassException(string str)
        {
            message = str;
        }
        string what()
        {
            return message;
        }

    private:
        string message;
    };
    class dayClassException
    {
    };
    class yearClassException
    {
    };


    header();

    do 
    {
        try
        {
            cout << "Please enter your date of birth (MM-DD-YYYY): " << endl;
            cin >> month;

            cin.ignore(10,'-');
            cin >> day;

            cin.ignore(10,'-');
            cin >> year;


            if (month > 12 || month < 1)
                throw monthClassException("Invalid Month Entry");

            if( ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30) || day < 1)
                throw dayClassException();

            else if ( ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 ) && day > 31) || day < 1)
                throw dayClassException();

            else if (month == 2 && year % 4 != 0 && day > 28)
                throw dayClassException();

            else if((month == 2 && year % 4 = 0) && day > 29)
                throw dayClassException();
        }
        catch(monthClassException mCEO)
        {
            cout << mCEO.what() << endl;
            system("pause");
        }
        catch(dayClassException)
        {
            cout << "Invalid Day Entered for Selected Month" << endl;
            system("pause");
        }
        catch(yearClassException yCEO)
        {
        }
    }while(!flag);


    return 0;
}

I'm getting my error at that very last exception:

            else if((month == 2 && year % 4 = 0) && day > 29)
                throw dayClassException();

it's saying that month is an invalid l-value (Why now? At the very end after I've already used it -catastrophically, I will admit.) It could be something really obvious that I fail to see because I'm the one who coded it, or it could be because my really really crazy if statements messed something up somewhere.

Any ideas?

WTFNo
  • 1
  • 1
  • 2

5 Answers5

5

Here is the error:

   year % 4 = 0

you probably meant to write ==

thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110
  • 1
    You're right... thanks >_<; wow, it's been like fifteen minutes of reading and re-reading and then, when I finally give in and ask, I load the question and SEE the problem myself lol. – WTFNo Mar 23 '14 at 18:26
0

= operator as in

year % 4 = 0

means assignment, not comparison. Hence your error. Fix it to

year % 4 == 0
Mauren
  • 1,955
  • 2
  • 18
  • 28
0

You have year % 4 = 0.
I think you have a typo: you may want year % 4 == 0.

In addition, I prefer using parentheses to make the code clearer:

...
else if ((month == 2) && (year % 4 == 0) && (day > 29)) {
    throw dayClassException();
}
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
0

A suggestion in this regard, is to always put the constant on the left hand side of a comparison statement. It helps prevent logical errors. As an example consider the code

    if year == 0 

and mistakenly you had written:

    if year = 0 

the result would have been a logical error. Instead putting the constant 0 on the left side, such that

     if 0 = year

would generate a syntax error at compilation, hence preventing you from committing a logical error (that can be harder to debug)

Hashim Sharif
  • 81
  • 1
  • 4
0

You have a the assignment operator = in your condition instead of the comparison operator ==.

That's pretty clearly a logic error. However, why is it a compiler error? After all, C++ allows assignment inside a condition, and that is something you might legitimately do.

In your case, month == 2 && year % 4 = 0 is processed as ((month == 2) && (year % 4)) = 0 (see C++ Operator Precedence). That expression in the parens evaluates to a temporary. But the left side of an assignment operator must refer to a storage address you can write to (an l-value). So your code is invalid for the same reason that 3 = 3 is invalid. Visual Studio calls this error C2106.

Community
  • 1
  • 1
Alex P
  • 1,559
  • 11
  • 23