Confused on Class problem

This is a discussion on Confused on Class problem within the C++ Programming forums, part of the General Programming Boards category; I have this one homework that has been bugging me for about week now, and I still can't figure it ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    2

    Confused on Class problem

    I have this one homework that has been bugging me for about week now, and I still can't figure it out. Its based on an earlier problem I did, but I just can't get it, I must be missing something. Well if you can help me figure this out I'd very much appreciate it.

    Modify the Date class in the chapter before. The new version shoudl have the following overloaded operators:

    ++ Prefix and postfix increment operators. These operators should increment the object's day member.
    -- Prefix and postfix decrement operators. These operators should decrement the object's day member.
    - Subtraction operator. If one Date object is subtracted from another the operator should give the number of days between the two dates. For example, if April 10, 2000 is subtracted from April 18, 2000, the result will be 8.
    << cout's stream insertion operator. This operator should cause the date to be dsiplayed in the form: April 18, 2000
    >> cin's extraction operator. This operator shoudl prompts the user for a date to be stored in the Date object.

    The class should etect the following conditions and handle them accordingly:
    when a date is et to the last day of the month and incremented, it should become the first day of the following month
    when a date is set to December 31 and incremented, it should become January 1 of the following year
    when a day is et to the first day of the month and decremented, it should become the last day of the previous month
    when a date is set to January 1 and decremented, it should become December 31 of the previous year




    I figured out the first part of this problem and here is the Date.h header file
    Code:
    #ifndef DATE_H
    #define DATE_H
    
    #include <string>
    #include <cctype>
    using namespace std;
    
    class Date
    {
     private:
      int month;
      int day;
      int year;
      char date[9];
      char name[10];
     public:
      Date() // empty constructor
      {
       month = 1;
       day = 1;
       year = 1900;
       strcpy(date,"");
       strcpy(name, "");
      }
    
      Date(int m, int d, int y) { setMonth(m);
              setYear(y);
              setDay(d);
              strcpy(date,"");
              strcpy(name, "");
    
             }
    
      void setMonth (int m) //checks if m is a valid month
      { if (m>0 && m < 13)
        month = m;
       else
        month=1;
      }
    
      void setYear(int y) //checks if y is a valid year
      {
       if (y>=1900)
        year=y;
       else
        year=1900;
      }
    
      void setDay(int d) //checks if d is a valid day
      {
       if (d>0 && d<=31)
        switch(month)
        {
         case 1: day=d;
           break;
         case 3: day=d;
           break;
         case 5: day=d;
           break;
         case 7: day=d;
           break;
         case 8: day=d;
           break;
         case 10: day=d;
            break;
         case 12: day=d;
            break;
    
    
         case 4: if (d<=30)
             day=d;
           else
           {
            day=1;
            break;
           }
         case 6: if (d<=30)
             day=d;
           else
           {
            day=1;
            break;
           }
         case 9: if (d<=30)
             day=d;
           else
           {
            day=1;
            break;
           }
         case 11: if (d<=30)
             day=d;
            else
            {
             day=1;
             break;
            }
    
    
         case 2: if (year%100==0)
           {
            if (year%400==0)
            {
             if (d<=29)
              day=d;
             else
              day=1;
            }
            else
            {
             if (d<= 28)
              day=d;
             else
              day=1;
            }
           }
           else
           {
            if (year%4==0)
            {
             if (d<=29)
              day=d;
             else
              day=1;
            }
            else
            {
             if (d<= 28)
              day=d;
             else
              day=1;
            }
            break;
           }
        }
      }
    
      char * getShortDate()
      {
       char strDay[3] = { '\0' };
       char strMon[3] = { '\0' };
       char strYr[3] = { '\0' };
    
       itoa(month, strMon, 10);
       strcpy(date, strMon);
       strcat(date, "/");
       itoa(day, strDay, 10);
       strcat(date, strDay);
       strcat(date, "/");
       itoa((year%100), strYr, 10);
       strcat(date, strYr);
       return date;
      }
    
      char * getMonthName(int m)
      {
    
       switch (m)
       {
        case 1: strcpy(name, "January");
          break;
        case 2: strcpy(name, "February");
          break;
        case 3: strcpy(name, "March");
          break;
        case 4: strcpy(name, "April");
          break;
        case 5: strcpy(name, "May");
          break;
        case 6: strcpy(name, "June");
          break;
        case 7: strcpy(name, "July");
          break;
        case 8: strcpy(name, "August");
          break;
        case 9: strcpy(name, "September");
          break;
        case 10: strcpy(name, "October");
           break;
        case 11: strcpy(name, "November");
           break;
        case 12: strcpy(name, "December");
              break;
       }
        return name;
      }
    };
    
    #endif
    And here is the Date.cpp file:

    Code:
    #include <iostream>
    #include "Date.h"
    
    using namespace std;
    
    int main()
    {
     Date date;
    
     int days, months, years;
    
     cout << "Which month would you like to display? " << endl;
     cin >>  months;
     date.setMonth(months);
     cout << "Which year would you like to display? " << endl;
     cin >> years;
     date.setYear(years);
     cout << "Which day would you like to display? " << endl;
     cin >> days;
     date.setDay(days);
    
     cout << endl;
     cout << "Here is the information: " << endl;
     cout << endl;
    
     cout << date.getMonthName(months) << " " << days << ", " << years << endl;
    
     return 0;
    }
    Any help is very much appreciated
    Last edited by Darth_Infamous; 04-26-2004 at 07:59 PM.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    use [code][/code] tags
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    2
    Can anyone help me?

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Yes, if you tell us the problem.

    Also, for your setDay() function, your switch statement can be shortened a LOT:
    Code:
    void setDay(int d) //checks if d is a valid day
      {
       if (d>0 && d<=31)
        switch(month)
        {
         case 1:
         case 3:
         case 5:
         case 7:
         case 8:
         case 10:
         case 12:
            day=d;
            break;
    
         case 4:
         case 6:
         case 9:
         case 11:
             if (d<=30)
                day=d;
             else
                day=1;
             break;
    
         case 2: if (year%100==0)
           {
            if (year%400==0)
            {
             if (d<=29)
              day=d;
             else
              day=1;
            }
            else
            {
             if (d<= 28)
              day=d;
             else
              day=1;
            }
           }
           else
           {
            if (year%4==0)
            {
             if (d<=29)
              day=d;
             else
              day=1;
            }
            else
            {
             if (d<= 28)
              day=d;
             else
              day=1;
            }
            break;
           }
        }
      }
    Also, you might want to look over your leap-year checking code, it looks slightly suspicious.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class problem
    By w274v in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2006, 06:53 PM
  2. Quaternion class problem
    By Lurker in forum C++ Programming
    Replies: 14
    Last Post: 11-18-2003, 06:01 PM
  3. intArray class problem!
    By nag in forum C++ Programming
    Replies: 3
    Last Post: 04-09-2003, 11:43 PM
  4. problem with sending files to a class method..
    By utoots in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2003, 01:38 AM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM

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