Thread: Homework Help - Create C++ Date class

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Don't you have some class member function that returns the numeric value that relates to the month?

    Jim

  2. #17
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    This is not valid C++ syntax:

    Code:
    d2.getMonthName(int)
    instead of "int," you need to give it an actual value, like d2.getMonth() or similar.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #18
    Registered User
    Join Date
    Mar 2014
    Posts
    27
    I've got that getMonth function which is slick and returns the integer value month, but adding month in my d2.getMonthName line returns "Blank" from when I declare, initialize name in Date_Hardesty.cpp.

  4. #19
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by boygenuis View Post
    I've got that getMonth function which is slick and returns the integer value month, but adding month in my d2.getMonthName line returns "Blank" from when I declare, initialize name in Date_Hardesty.cpp.
    1. Show the code. Your description can't convey the complexity of a line of code;

    2. My guess is that the value of month is outside the range recognized by getMonthName(), and therefore, none of the if() statements catch it. You should change it to a series of "if" and "else if" statements, and add a final "else" to catch values that aren't recognized.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #20
    Registered User
    Join Date
    Mar 2014
    Posts
    27
    Hmm, ok, so I've got my getMonthName function

    Code:
    //function to return the MonthName to main
    std::string Date::getMonthName (int m)
    {
       std::string name = "Blank";
    
       if (m == 1)
          name = "January";
       else if (m == 2)
          name = "February";
       else if (m == 3)
          name = "March";
       else if (m == 4)
          name = "April";
       else if (m == 5)
          name = "May";
       else if (m == 6)
          name = "June";
       else if (m == 7)
          name = "July";
       else if (m == 8)
          name = "August";
       else if (m == 9)
          name = "September";
       else if (m == 10)
          name = "October";
       else if (m == 11)
          name = "November";
       else if (m == 12)
          name = "December";
    
       return name;  //send the Month's name back to main
    } //end getMonthName
    And, I've got my main function
    Code:
    // DateDemo.cpp
    
    #include <iostream>
    #include <iomanip>
    #include <string>
    using std::cout;
    using std::endl;
    using std::setfill;
    using std::setw;
    using std::string;
    
    #include "Date_Hardesty.h"
    
    // note - you may need to change the definition of the main function to
    // be consistent with what your C++ compiler expects.
    
    //int _tmain(int argc, _TCHAR* argv[])
    int main()
    {
        int month;
        
        Date d1;             // default ctor
        Date d2(7, 4, 1976); // July 4'th 1976
        Date d3(0, 15, 1880);// Adjusted by ctor to January 15'th 1900
    
        d1.print();         // prints 01/01/2000
        d1.printLong();     // prints 1 January 2000
        cout << endl;
    
        d2.print();         // prints 07/04/1976
        d2.printLong();     // prints 4 July 1976
        cout << endl;
    
        d3.print();         // prints 01/15/1900
        d3.printLong();     // prints 15 January 1900
        cout << endl;
    
        cout << "object d2's day is " << d2.getDay() << endl;
        cout << "object d2's month is "  << d2.getMonth() << 
        " which is " << d2.getMonthName(month) << endl;
        cout << "object d2's year is " << d2.getYear() << endl;
    
        return 0;
    }
    But, I seem to be missing how to print the correct long version of my d2 Date. Do you need to see more code?

    Here's my output:
    01/01/2000
    01 January 2000

    07/04/1976
    04 July 1976

    00/15/1880
    15 Blank 1880

    object d2's day is 4
    object d2's month is 7 which is Blank
    object d2's year is 1976
    Last edited by boygenuis; 06-03-2014 at 09:55 AM. Reason: added output

  6. #21
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    You aren't initializing month to anything. It's just whatever happens to be at that location on the stack when it gets created. In line 40, instead of passing month to getMonthName(), pass d2.getMonth(). That should solve your problem. Alternately, assign a value to month, such as d2.getMonth().
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #22
    Registered User
    Join Date
    Mar 2014
    Posts
    27
    OH! I didn't know I could do that. That's neat. I appreciate your patience. Thank you.

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Hmmm. Considering you appear to be using Visual Studio, running the code should result in an error saying that month is uninitialized when used. There is a debugger in the IDE, you know... try it sometimes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. homework help: program to graph date...
    By Jazmin Jasso in forum C Programming
    Replies: 5
    Last Post: 04-29-2013, 08:55 PM
  2. Replies: 1
    Last Post: 05-07-2012, 11:39 PM
  3. Create struct tm from user specified date
    By cunzlow in forum C Programming
    Replies: 4
    Last Post: 09-30-2009, 12:39 PM
  4. Retreiving File create/modify date
    By super_stripey in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2004, 07:34 AM
  5. date class
    By aquaman in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2001, 09:57 AM

Tags for this Thread