Thread: date class

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    8

    Angry date class

    Hello,
    trying to write a simple date class and convert the user input date from something like 02 01 1987 to 02JAN1987. Any help would be greatlly appreciated.
    Aquaman

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Can you post the code that you have so far?
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    0x01
    Join Date
    Sep 2001
    Posts
    88

    If your trying to get the local time, then.

    time_t now; char * loctime;
    struct tm *ptr;

    time(&now);
    ptr = localtime(&now);

    loctime = asctime(ptr);

    printf("\nThe local time is: %s",loctime);

    // Should print something like:
    // The local time is: Sat Sep 15 16:49:12 2001

    Make sure you include this header file -> time.h

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    8

    class date

    #include <iostream>
    using namespace std;

    class date {
    int day;
    int month;
    int year;
    enum month {JAN = 1, FEB = 2, MAR = 3, APR = 4, MAY = 5, JUN = 6, JUL = 7,
    AUG = 8, SEP = 9, OCT = 10, NOV = 11, DEC = 12};



    public:
    Date( int = 1, int = 1, int = 2000 ); // default constructor
    void print();
    };

    date::date(int dd, int mm, int yyyy)
    {
    day = dd;
    month = mm;
    year = yyyy;
    }

    void date:rint()
    {
    cout << "The date is:" << endl;
    cout << "Day: " << day << endl;
    cout << "Month: " << month << endl;
    cout << "Year: " << year << endl;
    }

    typedef date Date;

    int main()
    {
    int day, month, year;
    Date today;

    cout << "Enter today's date in DD MM YYYY format: ";
    cin >> day >> month >> year;

    // today.init(day, month, year);
    // today.print();

    exit(0);
    }

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Instead of making an enum for the months, you could make an array of strings to hold the name of each month:

    char month[][] = { "JAN", "FEB", "MAR", ... etc ... };

    Then, to print it out, you make sure that the value for the month is 1 <= month <= 12, so then you would decrement that value by one, and print the corresponding month name:

    int _month;
    cin >> _month;
    cout << month[_month - 1];
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Two more comments:

    On this line: ' Date( int = 1, int = 1, int = 2000 ); // default constructor'
    The word Date is capitalized, but it isn't anywhere else. To get the program to compile correctly, make sure they are both spelled exactly the same (even capitalization).

    Also, you have a variable named month and an enum named month. One of these should change otherwise the compiler might become 'confused'.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    8
    Zach,
    thank you for your suggestions. But I am still getting errors. Is the syntax correct for this line of code? char month[][] = {"asddas...
    Also the print suggestion you gave me, should that go in the the body of the void date:rint() or int main()
    thanks again
    Michael

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  3. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  4. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM