Date class help!! Please

This is a discussion on Date class help!! Please within the C++ Programming forums, part of the General Programming Boards category; I need to make a member function nextday to increment the day by one. The Date object should always remain ...

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

    Date class help!! Please

    I need to make a member function nextday to increment the day by one. The Date object should always remain in a consistant state. Need code that tests the nextday function in a loop that prints the date during each iteration of the loop to illustrate that the nextday function works correctly. Have to test incrementing into the next month and next year.

    Here is what I have so far.

    Thanks a lot!!!!!!!





    #ifndef DATE1_H
    #define DATE1_H

    class Date {
    public:
    Date( int = 1, int = 1, int = 1900 ); // default constructor
    void print() const; // print date in month/day/year format
    ~Date(); // provided to confirm destruction order

    nextday(int,int,int);// increment values

    private:
    int month; // 1-12
    int day; // 1-31 based on month
    int year; // any year

    // utility function to test proper day for month and year
    int checkDay( int );

    };

    #endif




    // date1.cpp
    // Member function definitions for Date class.
    #include <iostream>

    using std::cout;
    using std::endl;

    #include "date1.h"

    // Constructor: Confirm proper value for month;
    // call utility function checkDay to confirm proper
    // value for day.
    Date::Date( int mn, int dy, int yr )
    {


    if ( mn > 0 && mn <= 12 ) // validate the month
    month = mn;
    else {
    month = 1;
    cout << "Month " << mn << " invalid. Set to month 1.\n";
    }

    year = yr; // should validate yr
    day = checkDay( dy ); // validate the day

    cout << "Date object constructor for date ";
    print(); // interesting: a print with no arguments
    cout << endl;
    }

    // Print Date object in form month/day/year
    void Date::print() const
    { cout << month << '/' << day << '/' << year; }

    Destructor: provided to confirm destruction order
    Date::~Date()
    {
    cout << "Date object destructor for date ";
    print();
    cout << endl;
    }

    ///////// This is my problem area need a loop to test here////////
    Date::nextday(int mn, int dy, int yr)
    { day += dy;
    month += mn;
    year += yr;
    print();
    }

    // Utility function to confirm proper day value
    // based on month and year.
    // Is the year 2000 a leap year?
    int Date::checkDay( int testDay )
    {
    static const int daysPerMonth[ 13 ] =
    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
    return testDay;

    if ( month == 2 && // February: Check for leap year
    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
    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    What are you testing for? Valid days/months, etc? If so, just call that validating function. Can you put the loop in main, instead of the class? It seems like you know how to do a loop.
    I'd say either put the loop in main, or if not, in the nextday function. In the loop increment, call the checkDay function, then the print function. You'll have to pick some arbitrary amount of times to loop, or some arbitrary event. Looping 367 times would be sure to hit the next year increment if you started with January first of some year, or 33 times if you start with the end of November.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    10
    Thank you for your reply!!!!

    Yes i am cheking for valid days and months, but i can't figure out how to set up my loop in main. I think that as the loop cycles through I have to pass the values to the increment function.

    I tried but my output only made it to Febuary!! I'm new at programming this is my second class. Not to good with control structures.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    Try something like:
    Code:
    int i = 1;
    Date testDay;  // Can initialize this
    while (i > 368) // or whatever value you want
     {
       testDay.nextday();
       testDay.print();
       i++;
     }
    Since checkDay is private, use that in the implementation of nextday. A for loop sems better than a while loop, though.

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, 10:33 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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