3
votes

Okay so I'm building a Date class

class Date
{
    private:
    int d;
    int m;
    int y;

 public:
    Date();
    Date(int&, int&, int&);
    Date(const Date&);
    bool isLeap();
    void setDay(int& day)
    {d = day;}
    void setMonth(int& month)
    {m = month;}
    void setYear(int& year)
    {y = year;}
    int getDay() const
    {return d;}
    int getMonth() const
    {return m;}
    int getYear() const
    {return y;}
    void print(ofstream& outfile)
    {
        outfile << "The date in this class is: " <<  << m << "/" << d << "/" << y;
    }

    Date operator= (const Date&);
};

I created an ofstream in main and opened the file. I'm also making sure to pass it by reference! Every time I try to compile I get the error: expected primary-expression before '<<' token message... Here is my main function. Let me know if I need anything else.

    int main()
{
    int day;
    int month;
    int year;
    Date d1;
    ofstream outfile("Print.txt");
    int maxDays;

    Date d2 = d1;

    cout << "Please enter the 4 digit year: ";
    cin >> year;
    if (year > 0)
    {
        d1.setYear(year);
        cout << "Please enter the 2 digit month: ";
        cin >> month;
        if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
            maxDays = 31;
        else if (month==4||month==6||month==9||month==11)
            maxDays = 30;
        else if (month==2&&d1.isLeap()==false)
            maxDays = 28;
        else if (month==2&&d1.isLeap()==true)
            maxDays = 29;
        if (month < 12)
        {
            d1.setMonth(month);
            cout << "Please enter the 2 digit day: ";
            cin >> day;
            if (day <= maxDays)
                d1.setDay(day);
            else
            {
                cout << "Invalid input, will set to default." << endl;
                d1 = d2;
            }


        }
        else
        {
            cout << "Invalid input, will set to default." << endl;
            d1 = d2;
        }
    }

    else
    {
        cout << "Invalid input, will set to default." << endl;
        d1 = d2;
    }

    d1.print(outfile);

    if(d1.isLeap()==true)
        cout << "This is a leap year\n";
    if(d1.isLeap()==false)
        cout << "This is not a leap year\n";

    cout << "The date is " << d1.getDay() << "/" << d1.getMonth() << "/" << d1.getYear() << endl;

    outfile.close();

    return 0;
}

Any help would be greatly appreciated...

2
You have << << without anything in between. - Code-Apprentice
One error is "outfile << "The date in this class is: " << << m << "/" << d << "/" << y;" There are two << without an expression between them - David D
This question is a prime example of why you should try to come up with a SSCCE. If you had done so, you'd have solved the problem by yourself, already! - Carl Norum

2 Answers

5
votes

This line:

outfile << "The date in this class is: " <<  << m << "/" << d << "/" << y;

Has two << with nothing between them.

Editorial note: I'm guessing you're using GCC, based on the error message you mention. You might be interested to try clang, which produces a much better error message in this case:

example.cpp:30:54: error: expected expression
    outfile << "The date in this class is: " <<  << m << "/" << d << "/" << y;
                                                     ^
1 error generated.

If you're a beginner, it might save you a lot of time over chasing down the bizarro GCC error messages that can be generated by minor syntax errors.

1
votes

Not what you asked for, but, if you rotate this around a little:

    if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
        maxDays = 31;
    else if (month==4||month==6||month==9||month==11)
        maxDays = 30;
    else if (month==2&&d1.isLeap()==false)
        maxDays = 28;
    else if (month==2&&d1.isLeap()==true)
        maxDays = 29;

Like this:

    if (month==2)
    {  
       if (d1.isLeap())
         maxDays = 29;
       else
         maxDays = 28; 
    }
    else if (month==4||month==6||month==9||month==11)
        maxDays = 30;
    else
        maxDays = 31;

your code gets quite a bit easier to read, in my view.