Thread: month showing as first three letter classes

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    95

    month showing as first three letter classes

    I've written a class which name is Month. As taking a number and assinging to month variable works well. But, If I enter first three letter of month, it doesn't work as expected. Is my calling function with char arguments wrong in void Month::inputAsCh() ?
    Define a class called Month that is an abstract data type for a month. Your class will have one member variable of type int to represent a month (1 for January, 2 for February, and so forth). Include all the following member functions: a constructor to set the month using the first three letters in the name of the month as three arguments, a constructor to set the month using an integer as an argument (1 for January, 2 for February, and so forth), a default constructor, an input function that reads the month as an integer, an input function that reads the month as the first three letters in the name of the month, an output function that outputs the month as an integer, an output function that outputs the month as the first three letters in the name of the month, and a member function that returns the next month as a value of type Month. The input and output functions will each have one formal parameter for the stream. Embed your class definition in a test program.

    Code:
    example input 11
    then
    apr but when i enter apr it is not shown right it shows jan

    Code:
    #include <iostream>
    using namespace std;
    
    
    class Month
    {
    public:
        Month (char letter1, char letter2, char letter3);
        Month (int numOfMonth);
        Month ();
        void inputAsNum(); // read month as integer
        void inputAsCh(); //  read first three chars of month
        void outputAsCh();
        void outputAsNum();
        Month nextMonth();
    private:
        int month;
    };
    
    
    int main()
    {
        Month test;
        test.inputAsNum();
        cout << "Current month is " << endl;
        test.outputAsCh();
        test.outputAsNum();
        cout << endl;
        test.nextMonth();
        cout << "Next month is " <<endl;
        test.outputAsCh();
        test.outputAsNum();
        cout << endl;
        
        test.inputAsCh();
        cout << "Current month is " << endl;
        test.outputAsCh();
        test.outputAsNum();
        cout << endl;
        test.nextMonth();
        cout << "Next month is " <<endl;
        test.outputAsCh();
        test.outputAsNum();
        cout << endl;
        
        return 0;
    }
    
    
    Month::Month (char letter1, char letter2, char letter3)
    {
        if ((letter1 == 'j')&&(letter2 == 'a')&&(letter3 == 'n'))
            month= 1;
        else if ((letter1 == 'f')&&(letter2 == 'e')&&(letter3 == 'b'))
            month= 2;
        else if ((letter1 == 'm')&&(letter2 == 'a')&&(letter3 == 'r'))
            month= 3;
        else if ((letter1 = 'a')&&(letter2 == 'p')&&(letter3 == 'r'))
            month= 4;
        else if ((letter1 == 'm')&&(letter2 == 'a')&&(letter3 == 'y'))
            month= 5;
        else if ((letter1 == 'j')&&(letter2 == 'u')&&(letter3 == 'n'))
            month= 6;
        else if ((letter1 == 'j')&&(letter2 == 'u')&&(letter3 == 'l'))
            month= 7;
        else if ((letter1 == 'a')&&(letter2 == 'u')&&(letter3 == 'g'))
            month= 8;
        else if ((letter1 == 's')&&(letter2 == 'e')&&(letter3 == 'p'))
            month= 9;
        else if ((letter1 == 'o')&&(letter2 == 'c')&&(letter3 == 't'))
            month= 10;
        else if ((letter1 == 'n')&&(letter2 == 'o')&&(letter3 == 'v'))
            month= 11;
        else if ((letter1 == 'd')&&(letter2 == 'e')&&(letter3 == 'c'))
            month= 12;
    }
    Month::Month (int numOfMonth)
                        :month(numOfMonth)
    { }
    Month::Month ()
                        :month(1)
    { }
    void Month::inputAsNum()
    {
        int num;
        cout << "Enter num of month => ";
        cin  >> num;
        month = num;
    }
    void Month::inputAsCh()
    {
        char c1,c2,c3;
        cout << "Enter three letters of month => ";
        cin  >> c1 >> c2 >> c3;
        Month::Month(c1,c2,c3);
    }
    void Month::outputAsCh()
    {
        if (month == 1)
            cout << "Jan ";
        else if (month == 2)
            cout << "Feb ";
        else if (month == 3)
            cout << "Mar ";
        else if (month == 4)
            cout << "Apr ";
        else if (month == 5)
            cout << "May ";
        else if (month == 6)
            cout << "Jun ";
        else if (month == 7)
            cout << "Jul ";
        else if (month == 8)
            cout << "Aug ";
        else if (month == 9)
            cout << "Sep ";
        else if (month == 10)
            cout << "Oct ";
        else if (month == 11)
            cout << "Nov ";
        else if (month == 12)
            cout << "Dec ";
    }
    void Month::outputAsNum()
    {
        cout << month;
    }
    Month Month::nextMonth()
    {
        if (month < 12)
            month++;
        else if (month == 12)
            month = 1;
        return Month(month);
    }

    Last edited by Ph0x; 08-13-2015 at 04:06 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think the biggest problem is basically that you aren't using the data of the class to change test. Notice that you write Month test; which calls the constructor below:
    Code:
    Month::Month ()
    
                        :month(1)
    
    { }
    Do you see the variable month in there? When an object is going to be changed by a method, the method needs to change its "month".

    The problem is most of the functions that should change the test object end up creating new objects instead. You need to rewrite the class, and focus on changing the month variable accordingly. When you do that, everything should work.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Maybe the post was edited later, but I don't think the class needs to be rewritten. It looks like there's just one line that's a problem inside inputAsCh(). It's trying to reconstruct the current object. That doesn't work. You can either construct a temporary object and assign it to the current one (kludgy) or you can separate the code from the three char constructor into it's own method and call that method from both places.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 11-24-2012, 04:10 AM
  2. One month calendar
    By danieldcc in forum C Programming
    Replies: 11
    Last Post: 07-13-2011, 03:18 PM
  3. How To Make Capital Letter To Small Capital Letter??
    By jason07 in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2005, 04:37 PM
  4. getting the next day's day, month, year etc
    By underthesun in forum C Programming
    Replies: 3
    Last Post: 02-17-2005, 07:43 AM
  5. Big Letter became small letter
    By cogeek in forum C Programming
    Replies: 27
    Last Post: 12-13-2004, 02:04 PM