Thread: ONE ERROR IN C++ Program

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    26

    ONE ERROR IN C++ Program

    I am trying to increment the days to change over to the next day and year. Here is my code so far, but I keep getting one error.

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    // class Date definition
    class Date {
    
    public:
       Date( int = 1, int = 1, int = 1990 ); // default constructor
      void print(); 
    
    private:
       int month;
       int day;
       int year;
    
    }; // end class Date
    
    // Date constructor with range checking
    Date::Date( int m, int d, int y )
    {
    
       void incrementDay( Date &, const int );  // prototype
    int main()
    
    {//
       Date t;              // create Date object
    
       // set time using individual set functions
       t.setMonth( 17 );     // set month to valid value
       t.setDay( 34 );   // set day to valid value
       t.setYear( 25 );   // set year to valid value
    
       // use get functions to obtain month, day and year
       cout << "Result of setting all valid values:\n" 
            << "  Month: " << t.getMonth()
            << "  Day: " << t.getDay()
            << "  Year: " << t.getYear();
    
       // set date using individual set functions
       t.setMonth( 234 );    // invalid month set to 0
       t.setDay( 43 );   // set day to valid value
       t.setYear( 6373 ); // invalid year set to 0
    
       // display month, day and year after setting 
       // invalid day and year values
       cout << "\n\nResult of attempting to set invalid Date:\n"
            << " month:\n  Month: " << t.getMonth()
            << "  Day: " << t.getDay()
            << "  Year: " << t.getYear() << "\n\n";
    
       t.setDate( 11, 30,2000 );    // set time
       incrementDay( t, 3 );  // increment t's day by 3
    
       return 0;
    
    }
    } // end constructor Date
    
    // print Date in the format mm-dd-yyyy
    void Date::print() 
    { 
       cout << month << '-' << day << '-' << year; 
    
    } // end function print
    
    int main()
    {
       Date date1( 7, 4, 2002 );
       Date date2;  // date2 defaults to 1/1/1990
    
       cout << "date1 = ";
       date1.print();
       cout << "\ndate2 = ";
       date2.print();
    
       date2 = date1;   // default memberwise assignment
    
       cout << "\n\nAfter default memberwise assignment, date2 = ";
       date2.print();
       cout << endl;
    
       return 0;
    
    }
    here is the error code I get.

    C:\Documents and Settings\Local Settings\Temp\Fig06_24.cpp(30) : error C2601: 'main' : local function definitions are illegal
    Error executing cl.exe.

    A friend of mine also recommended putting another member function? in that runs the test.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    First thing I noticed is that you have two int main() functions.
    The first one you are trying to define INSIDE Date::Date( int m, int d, int y ) function which is NOT legal. You can not define a function inside another function.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    There should not be a main function in the Date constructor.

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    26
    Quote Originally Posted by Princeton
    There should not be a main function in the Date constructor.
    When I remove it, I get 15 errors saying all of the functions are not part of Date. How do I make them part of date?

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    Does your code program look like this?
    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    // class Date definition
    class Date {
    public:
       Date( int = 1, int = 1, int = 1990 ); // default constructor
      void print(); 
    
    private:
       int month;
       int day;
       int year;
    }; // end class Date
    
    // Date constructor with range checking
    Date::Date( int m, int d, int y )
    {
      month = m;
      day = d;
      year = y;
    } // end constructor Date
    
    // print Date in the format mm-dd-yyyy
    void Date::print() 
    { 
       cout << month << '-' << day << '-' << year; 
    } // end function print
    
    int main()
    {
       Date date1( 7, 4, 2002 );
       Date date2;  // date2 defaults to 1/1/1990
    
       cout << "date1 = ";
       date1.print();
       cout << "\ndate2 = ";
       date2.print();
    
       date2 = date1;   // default memberwise assignment
    
       cout << "\n\nAfter default memberwise assignment, date2 = ";
       date2.print();
       cout << endl;
    
       return 0;
    }

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Post those errors.

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    26
    Quote Originally Posted by Princeton
    Does your code program look like this?
    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    // class Date definition
    class Date {
    public:
       Date( int = 1, int = 1, int = 1990 ); // default constructor
      void print(); 
    
    private:
       int month;
       int day;
       int year;
    }; // end class Date
    
    // Date constructor with range checking
    Date::Date( int m, int d, int y )
    {
      month = m;
      day = d;
      year = y;
    } // end constructor Date
    
    // print Date in the format mm-dd-yyyy
    void Date::print() 
    { 
       cout << month << '-' << day << '-' << year; 
    } // end function print
    
    int main()
    {
       Date date1( 7, 4, 2002 );
       Date date2;  // date2 defaults to 1/1/1990
    
       cout << "date1 = ";
       date1.print();
       cout << "\ndate2 = ";
       date2.print();
    
       date2 = date1;   // default memberwise assignment
    
       cout << "\n\nAfter default memberwise assignment, date2 = ";
       date2.print();
       cout << endl;
    
       return 0;
    }
    Theres not going to be any errors with this, this is the code straight out of the book, that I have to modify. It has to increment,(which I thought I had done) and it has to perform an error check on invalid initializer values for month day and year.
    Carly

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    26

    New attempt

    ok, i took it on myself to do some changes to the program and I do get output now. I have a cpp. file and a headerfile. If anyone has a compliler and can run this and tell me what I am doing wrong, I would appreciate it. I think it is in the If statements. Here is the header

    Code:
    #ifndef DATEHEAD_H 
    #define DATEHEAD_H 
    class Date{
    public:
    Date( int = 1, int = 1, int = 1); // default constructor
    // set functions
    void setDate( int, int, int ); // set month, day, year
    void setMonth( int ); // set month
    void setNextDay( int ); // set day
    void setYear( int ); // set year
    
    // get functions
    int getDate(int,int,int);
    int getMonth(); // return month
    int getNextDay(); // return day
    int getYear(); // return year
    
    void printUniversal(); // output universal-time format
    void printStandard(); // output standard-time format
    private:
    int month; // 1 - 12 (24-hour clock format)
    int nextday; // 1 - 31
    int year; // 2000 - 2004
    
    }; // end clas Time
    #endif
    and here is the code I wrote
    Code:
    #include <iostream>
    using std::cout;
    #include <iomanip>
    using std::setfill;
    using std::setw;
    // include definition of class Time from time3.h
    #include "datehead.h"
    // constructor function to initialize private data;
    // calls member function setTime to set variables;
    // default values are 0 (see class definition)
    Date::Date( int month, int nextday, int year ) 
    { 
    setDate( month, nextday, year ); 
    } // end Time constructor
    // set hour, minute and second values
    void Date::setDate( int m, int d, int y )
    {
    setMonth( m );
    setNextDay( d );
    setYear( y );
    } // end function setTime
    // set hour value
    void Date::setMonth( int m ) 
    {
    month = ( m >= 1 && m < 12 ) ? m : 0; 
    } // end function setHour
    // set minute value
    void Date::setNextDay( int d )
    { 
    nextday = ( d >= 1 && d < 31 ) ? d : 0; 
    } // end function setMinute
    // set second value
    void Date::setYear( int y )
    { 
    year = ( y >= 2003 && y < 2004 ) ? y : 0; 
    } // end function setSecond
    // return hour value
    int Date::getMonth() 
    { 
    return month; 
    } // end function getHour
    // return minute value
     // end function getMinute
    // return second value
    int Date::getYear() 
    { 
    return year;
    } // end function getSecond
    // print Time in universal format
    int Date::getNextDay()
    {
    	return nextday;
    }
    void Date::printUniversal()
    {
    cout << setfill( '0' ) << setw( 2 ) << month << "/"
    << setw( 2 ) << nextday << "/"
    << setw( 2 ) << year;
    } // end function printUniversal
    // print Time in standard format
    void Date::printStandard()
    {
    cout << ( ( month == 0 || month == 12 ) ? 12 : month % 12 )
    << "/" << setfill( '0' ) << setw( 2 ) << nextday
    << "/" << setw( 2 ) << year
    ;
    } // end function printStandard
    
    
    // Demonstrating the Time class set and get functions
    #include <iostream>
    using std::cout;
    using std::endl;
    // include definition of class Time from time3.h
    #include "datehead.h"
    void incrementDay( Date &, const int ); // prototype
    int main()
    {
    Date t; // create Time object
    t.setDate( 12,28, 2003 ); // set time
    incrementDay( t,35 ); // increment t's ticks by 6
    return 0;
    } // end main
    // add specified number of minutes to a Time object
    void incrementDay( Date &tt, const int count )
    {
    cout << "Incrementing Date " << count
    << " times:\nStart time: ";
    tt.printStandard();
    for ( int i = 1; i < count; i++ ) {
    if ( tt.getNextDay() >= 0 )
    tt.setNextDay( ( tt.getNextDay() + 1 ) % 31 );
    if (tt.getNextDay() >31)
    tt.setMonth ( ( tt.getMonth() + 1)); 
    if (tt.getMonth() ==0)
    tt.setMonth( (tt.getMonth()+1)% 12);
     
    if ( tt.getYear()==0)
    tt.setYear( ( tt.getYear() +1) % 2006);
    
    
    cout << "\ndate : ";
    
    tt.printStandard();
    } // end for
    cout << endl;
    } // end function incrementTi
    Any help is appreciated, Carly

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    You are doing things the hard way I am afraid. Compare this with what you have.
    Code:
    #include <iostream>
    
    class Date {
      int month, day, year;
      static int mdays[2][13];
    
      int leap() const { return year % 4 == 0 && year % 100 != 0 || year %400 == 0; }
      friend std::ostream& operator<<(std::ostream& out, const Date& d);
    public:
      Date(int m = 0, int d = 0, int y = 1970);
      Date& operator++();
      const Date operator++(int);
    };
    
    int Date::mdays[2][13] = {
      0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
      0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    };
    
    Date::Date(int m, int d, int y)
      : month(m), day(d), year(y)
    {}
    
    Date& Date::operator++()
    {
      if (++day > mdays[leap()][month]) {
        if (++month > 12) {
          ++year;
          month = 1;
        }
        day = 1;
      }
    
      return *this;
    }
    
    const Date Date::operator++(int)
    {
      const Date old = *this;
    
      ++(*this);
    
      return old;
    }
    
    std::ostream& operator<<(std::ostream& out, const Date& d)
    {
      return out << d.month <<'/'<< d.day <<'/' << d.year;
    }
    
    int main()
    {
      Date now(12, 28, 2003);
    
      for (int i = 0; i < 70; i++) {
        std::cout <<"date: "<< ++now << std::endl;
      }
    
      return 0;
    }

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    26
    Quote Originally Posted by Princeton
    You are doing things the hard way I am afraid. Compare this with what you have.
    Code:
    #include <iostream>
    
    class Date {
      int month, day, year;
      static int mdays[2][13];
    
      int leap() const { return year % 4 == 0 && year % 100 != 0 || year %400 == 0; }
      friend std::ostream& operator<<(std::ostream& out, const Date& d);
    public:
      Date(int m = 0, int d = 0, int y = 1970);
      Date& operator++();
      const Date operator++(int);
    };
    
    int Date::mdays[2][13] = {
      0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
      0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    };
    
    Date::Date(int m, int d, int y)
      : month(m), day(d), year(y)
    {}
    
    Date& Date::operator++()
    {
      if (++day > mdays[leap()][month]) {
        if (++month > 12) {
          ++year;
          month = 1;
        }
        day = 1;
      }
    
      return *this;
    }
    
    const Date Date::operator++(int)
    {
      const Date old = *this;
    
      ++(*this);
    
      return old;
    }
    
    std::ostream& operator<<(std::ostream& out, const Date& d)
    {
      return out << d.month <<'/'<< d.day <<'/' << d.year;
    }
    
    int main()
    {
      Date now(12, 28, 2003);
    
      for (int i = 0; i < 70; i++) {
        std::cout <<"date: "<< ++now << std::endl;
      }
    
      return 0;
    }
    Thanks for the help but my assignment was to modify, actually my first post above to do this. I dont even know for sure that I am supposed to have a separate header file, but i learned it on my last assignment and got it to work fine with counting a clock and changing from am to pm. Ive just got to figure out the same way for it to do years months and days. Thanks anyway.

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >>Thanks anyway.
    I see. I am sorry that my code was no help at all to you.

  12. #12
    Registered User
    Join Date
    Jul 2004
    Posts
    26
    Quote Originally Posted by Princeton
    >>Thanks anyway.
    I see. I am sorry that my code was no help at all to you.
    no, I understood your code and it was a help but not on this particular project. I need mine to just rollover the year, month and day like it is supposed to. our chapter is on referencing and not so much on some of the things that were included in your code. but seriously thanks anyway. I think Ive about got it. it just keeps printing the same year, and the same month. the day is changing fine.

  13. #13
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >>I need mine to just rollover the year, month and day like it is supposed to.
    I honestly do not see a problem. The code I gave you does precisely that. In fact, you can simplify the interface without modifying the implementation one bit. Here is an example that does what you want, yet still handles the days in a month incorrectly and does not account for leap year. Clearly that is what you wanted.
    Code:
    #include <iostream>
    
    class Date {
      int month, day, year;
    public:
      Date(int m = 0, int d = 0, int y = 1970);
      void increment();
      void print() const;
    };
    
    Date::Date(int m, int d, int y)
    {
      month = m;
      day = d;
      year = y;
    }
    
    void Date::increment()
    {
      if (++day > 31) {
        if (++month > 12) {
          ++year;
          month = 1;
        }
        day = 1;
      }
    }
    
    void Date::print() const
    {
      std::cout << month <<'/'<< day <<'/' << year << std::endl;
    }
    
    int main()
    {
      Date now(12, 28, 2003);
    
      for (int i = 0; i < 70; i++) {
        now.print();
        now.increment();
      }
    
      return 0;
    }

  14. #14
    Registered User BillBoeBaggins's Avatar
    Join Date
    Oct 2003
    Posts
    107
    Princeton, just wanted to say your code is piece of beauty. Looks like something you see in a book. Thats it.

    daisy244, this might be terrible advice, but in order for me to do away with the messy function naming get/set I usually name the same and the one that is "get" returns the value with no paramaters, the "set" returns void and has a parameter for the type you want to set it to.

    Code:
    void Date::Month(int i)
    {
    
    }
    int Date::Month(void)
    {
    }
    May the compiler be with you.
    Be one with the compiler, and you shall prosper greatly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM