Incrementing time and calendarfrom 1999-2000

This is a discussion on Incrementing time and calendarfrom 1999-2000 within the C++ Programming forums, part of the General Programming Boards category; I want to increment time from dec 31,1999 23:59:59 to Jan 1,2000 00:00:01.We have to use validation for the year ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    2

    Incrementing time and calendarfrom 1999-2000

    I want to increment time from dec 31,1999 23:59:59 to Jan 1,2000 00:00:01.We have to use validation for the year month and day and time min sec..etc.
    Any suggestions on how to go on from here using classes and such? Any help would be greatly appreciated.Thanks!Here is an example of my code:

    #include<iostream.h>
    #include<iomanip.h>


    class date
    {
    int day;
    int month;
    int year;

    public:
    date( int d= 31, int m= 12, int y= 1999)
    {
    status = true;
    setDate(d, m, y);
    print();
    }
    void setDate(int d, int m, int y)
    {
    int arr[13]= {0.31,28,31,30,31,30,31,31,30,31,30,31};
    year = y;
    if (m <= 12 && m > 0)
    {
    month = m;
    if (d > 0 && d <= arr[m])
    {
    day = d;

    }
    else
    {
    day =0;year = 0; month= 0;status= false;
    }

    }
    else
    {
    day =0;year = 0; month= 0;status= false;
    }
    }
    bool status;

    void print()
    {
    cout<< setw(10)
    << status
    << setw(10)
    << day
    << setw(10)
    << month
    << setw(10)
    << year
    <<endl;
    }

    };

    class time: public date
    {
    int hours;
    int mins;
    int secs;
    public:
    time(){int hours=0,int mins=0,int secs=0;}
    //this is the trouble spot!!!!


    void main()
    {
    date d(11,2,1999);
    d.setDate(1,1,2000);
    d.print();
    /* time t;
    t.tick(1);
    */
    }

    I am just not sure of where to go in the time dept.Any little push would help alot!

  2. #2
    Unregistered
    Guest
    Here's some ideas to start.

    Take all the information in the string and separate it out (parse it) into individual variables. Use whatever you wish (enums, whatever) to convert month names into month numbers.

    Now add one second to the second variable. If the seconds are greater than 59 subtract sixty from the number of seconds and store the result back in the seconds variable and add one to the minutes variable. If the minute variable is then greater than 59, subtract 60 from the minute variable and store result in the minute variable and add one to the hour. If the hour is greater than 23 then subtract 24 from the hour variable and store result in hour variable and increment the date variable. Look at the month and year variable to detemine if the new date variable is valid. If not reset the date variable and increment the month. If the month is greater than 12 then subtract 12 from the month variable and store the result in the month variable and increment the year variable.

    There are numerous posts on this site talking about how to determine if a day is valid for a given month and year.

Popular pages Recent additions subscribe to a feed

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