Undeclared Identifier

This is a discussion on Undeclared Identifier within the C++ Programming forums, part of the General Programming Boards category; This date program is supposed to take in the written date Code: Date g( January, 12, 1994 ); // written ...

  1. #1
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53

    Undeclared Identifier

    This date program is supposed to take in the written date
    Code:
    Date g( January, 12, 1994 ); // written date
    and convert it, displaying it as 01/12/94 and 12/1994 (ordinal, or as some call it, Julian). I'm getting the error "January, undeclared identifier". I'm not sure what is wrong? Thanks in advance for any info.

    Code:
    #include <iostream>
    #include <iomanip> // for setfill and setw
    #include <cstring> // for strings
    
    using namespace std;
    
    class Date // typically stored in header file date1.h
    {
       public:
          Date( const char* , int , int ); // constructor for January 15, YYYY
          void printJulian() const;  // print date in DDD YYYY format
          void printMDY() const;  // print date in MM/DD/YY
          void printLong() const;  // print date something like June 14, 2006
          ~Date();  // provided to confirm destruction order
    	     
       private:
          int month;
          int day;
          int year;
          int julian;
          int checkDay( int ) const; // function tests proper day
          int getMonthNumber( const char* ) const; // to compare January to 1 
    
    
    }; // end class Date; end of date1.h header file
    
       const char* monthTable[13] = {"invalid","January","February","March","April","May","June","July","August","September","October","November","December"
    
    }; // global array
    
    
    Date::Date(const char* _month, int _day, int _year) // untested
    {
       year = _year;                 // should validate yr
       month = getMonthNumber(_month);
       day = checkDay( _day );      // validate the day
    }
    
    void Date::printJulian() const // prints as DDD YYYY (ordinal/gregorian)
    {
       int calculator = 0;
    
       if (month == 1)
          calculator = 0;
       else if (month == 2)
          calculator = 31;
       else if (month == 3)
          calculator = 59;
       else if (month == 4)
          calculator = 90;
       else if (month == 5)
          calculator = 120;
       else if (month == 6)
          calculator = 151;
       else if (month == 7)
          calculator = 181;
       else if (month == 8)
          calculator = 212;
       else if (month == 9)
          calculator = 243;
       else if (month == 10)
          calculator = 273;
       else if (month == 11)
          calculator = 304;
       else if (month == 12)
          calculator = 334;
    
       int gregorian;
       gregorian = (day + calculator);
       cout << setfill('0') << setw(3) << gregorian << "/" << year;
    
    
    } // end function printJulian
    
    void Date::printMDY() const // prints as MM/DD/YY
    {
       cout << setfill('0') << setw(2) << month << '/' << setw(2) << day << '/' << setw(2) << year; 
    
    } // end function print3
    
    void Date::printLong() const // prints, for example, March 12, 1988 - written format
    {
    	const char* monthArray[13] = {"null","January","February","March","April","May","June","July",
    		"August","September","October","November","December"};
    
        cout << monthArray[month] << " " << day << ", " << year; 
    
    } // end function print4
    
    // output Date object to show when its destructor is called
    Date::~Date()
    { 
    
    } // end ~Date destructor
    
    // utility function to confirm proper day value based on 
    // month and year; handles leap years, too
    int Date::checkDay( int testDay ) const
    {
       static const int daysPerMonth[ 13 ] = 
          { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    
       // determine whether testDay is valid for specified month
       if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
          return testDay;
    
       // February 29 check for leap year 
       if ( month == 2 && testDay == 29 &&
          ( year % 400 == 0 ||                      
             ( year % 4 == 0 && year % 100 != 0 ) ) )
          return testDay;
    
       cout << "Day " << testDay << " invalid. Set to day 1.\n";
    
       return 1;  // leave object in consistent state if bad value
    
    } // end function checkDay; end of date1.cpp
    
    int Date::getMonthNumber(const char* _month) const
    {
       int key = 0;
          for (int i = 1; i< 13;i++){
             if (!strcmp(_month,monthTable[i]))
             {
                key = i;
                break;
             }
          }
       return key;
    }
    
    
    int main()
    {
    
       Date g( January, 12, 1994 ); // fourth date, written
    
       cout << "\nFourth date entered is: ";
       g.printLong();
        cout << "\nFourth date in DDD/YYYY (Gregorian) format: ";
       g.printJulian();
       cout << "\nFourth date in MM/DD/YY format: ";
       g.printMDY();
       cout << "\nFourth date in written format: ";
       g.printLong();
       cout << endl;
    
       return 0;
    
    } // end main
    THE redheaded stepchild.

  2. #2
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    Code:
    Date g( January, 12, 1994 );
    The function takes written date being the first argument a c-string as you can see in it's declaration:
    Code:
    Date( const char* , int , int );
    So try calling it as:
    Code:
    Date g("January",12,1994);
    As without the "" the compiler assumes January is the name of a variable. This ought to do the trick...

    cheers
    Last edited by LinuxCoder; 04-08-2006 at 08:10 AM.

  3. #3
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53
    Thank you so much, yes it works! Glad to see it was simple.
    THE redheaded stepchild.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 10:15 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 10:23 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21