homework help - overloaded constructor taking char or int

This is a discussion on homework help - overloaded constructor taking char or int within the C++ Programming forums, part of the General Programming Boards category; The whole point of the homework is to test an overloaded constructor. If I manually put an int into the ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    37

    homework help - overloaded constructor taking char or int

    The whole point of the homework is to test an overloaded constructor. If I manually put an int into the constructor it works and I get the correct result BUT I cannot figure out how to get it to take a set of 3 characters like "jun" or "dec".

    If I leave the constructor empty, it runs the default constructor like it should.
    If I put in a "2", February comes out like it should.
    But if I put in "jan" or any other set of 3 characters I get a blank.

    Code:
    #include <iostream>
    using namespace std;
    class month
    {
      private:
        int mnth;
    
      public:
        month();  //default constructor
        month(char newMonth);  //overloaded constructor
        month(int newMonthInt);  //overloaded constructor
        int getMonth();  //accessor
        void setMonth(int userMonth);  //mutator
        void displayMonth();
    };
    
    
    void main () 
    {
      month test;
    
      test = month();  //enter month here by int or 3char
    
      test.displayMonth();
    }
    
    
    void month::setMonth(int userMonth)
    {
      mnth = userMonth;
    }
    
    
    int month::getMonth()
    {
      return mnth;
    }
    
    
    month::month()
    {
      mnth = 1;
    }
    
    
    month::month(int userMonthInt)
    {
      mnth = userMonthInt;
    }
    
    
    month::month(char newMonth)
    {
      if (newMonth == 'jan')
        mnth = 1;
      else if (newMonth == 'feb')
        mnth = 2;
      else if (newMonth == 'mar')
        mnth = 3;
      else if (newMonth == 'apr')
        mnth = 4;
      else if (newMonth == 'may')
        mnth = 5;
      else if (newMonth == 'jun')
        mnth = 6;
      else if (newMonth == 'jul')
        mnth = 7;
      else if (newMonth =='aug')
        mnth = 8;
      else if (newMonth == 'sep')
        mnth = 9;
      else if (newMonth == 'oct')
        mnth = 10;
      else if (newMonth == 'nov')
        mnth = 11;
      else if (newMonth == 'dec')
        mnth = 12;
    }
    
    void month::displayMonth()
    {
      if (mnth == 1)
        cout << "January";
      else if (mnth == 2)
        cout << "February";
      else if (mnth == 3)
        cout << "March";
      else if (mnth == 4)
        cout << "April";
      else if (mnth == 5)
        cout << "May";
      else if (mnth == 6)
        cout << "June";
      else if (mnth == 7)
        cout << "July";
      else if (mnth == 8)
        cout << "August";
      else if (mnth == 9)
        cout << "September";
      else if (mnth == 10)
        cout << "October";
      else if (mnth == 11)
        cout << "November";
      else if (mnth == 12)
        cout << "December";
    
      cout << " was entered." << endl;
    }
    Thanks in advance for any advice or help.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,689
    If I leave the constructor empty, it runs the default constructor like it should.
    That's good.

    If I put in a "2", February comes out like it should.
    Really? Because "2" is a string. It shouldn't work, if in fact you mean "2" and not the number.

    But if I put in "jan" or any other set of 3 characters I get a blank.
    I think this is related to my earlier comment. 'jan' isn't a string either. It's a multibyte character constant. Compilers usually warn when you are using one that does not exist. Are your warnings turned on to the highest level? It really helps. Unfortunately you need to fix this everywhere now though.

    There is another problem with months argument before this can work. The parameter should be changed to char * (and compared with strcmp), or since this is C++, just use std::string. String always saves a bunch of headaches.
    Last edited by whiteflags; 10-05-2010 at 10:06 PM.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    37
    I would love to use a string...but those aren't covered until next chapter so that still kind of leaves me in the dark. I was originally trying to take input from the console, but that just further complicated things and since I don't HAVE TO read from the console I figured just manually entering the info would be easier. I understand where the disconnect is and why it doesn't properly function, I just don't really know where to go from here.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,689
    Well you're using string literals, so something like:
    Code:
    month::month (char *newMonth)
    {
       if (strcmp(newMonth, "jan") == 0)
          mnth = 1;
       else if (strcmp(newMonth, "feb") == 0)
          mnth = 2;
       // ... and so on
    }
    Something like that would be the minimum requirement.

    If you can't do that for some reason, that's OK, just don't make a class that can build off of a string like you've been trying. Use the numbers.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    37
    If I were to use the strcmp what would my syntax be for the input in the constructor? And why are they all == 0? I don't follow the logic.

    The assignment is to have an overloaded constructor that takes the month by either int or 3 char am I totally missing the point behind this then?

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,689
    The assignment is to have an overloaded constructor that takes the month by either int or 3 char am I totally missing the point behind this then?
    I highly doubt I'm wrong. 3 char if you take that literally is just a multicharacter constant. And you know that doesn't work. 3 char if you interpret it to mean "use a string" then it at least makes sense.

    If I were to use the strcmp what would my syntax be for the input in the constructor? And why are they all == 0? I don't follow the logic.
    strcmp is just a function, not attached to any class or anything. You'd call it like any other function, but you have to know what it returns and why to understand how it works. Sorry, I didn't explain that. I was hoping you'd go and research that, I guess. No problemo though.

    I really don't know what else to tell you, though.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    37
    Well on a positive note, what you suggested works. I understand the syntax now that it worked, before I must have typoed something because it didn't work and it left me lost. The homework isn't due for a few days so I'll present it to the professor and if he doesn't like how I did it I'll just see how he wants it. Thanks for the help man, I appreciate it.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    You really should make those functions accept const char* instead of char* because you are not going to modify them (const correctness).
    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. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 07:19 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

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