I need help understanding this

I need help understanding why this wont print out the months. I am getting the month numbers but not the strings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "Month.h"
  
using namespace std;



int main()
{
    Month m1;
    Month m2(3);
    Month m3("Apr");
    Month m4 = m3.nextMonth();

    cout << m1.getMonthNum() << endl; //Outputs 1
    cout << m2.getMonthString() << endl; //outputs Mar
    cout << m3.getMonthNum() << endl; // Outputs 4
    cout << m4.getMonthString() << endl; // Outputs May

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

#include "Month.h"


using namespace std;

Month::Month()
{
    MonthNum = 1;
}
Month::Month(int NewMonthNum)
{
    MonthNum = NewMonthNum;
}
Month::Month(string NewMonthString)
{
    MonthString = NewMonthString;

    if ( MonthString == "Jan")

        MonthNum =1;

    else if (MonthString == "Feb")

        MonthNum = 2;

    else if (MonthString == "Mar")

        MonthNum = 3;

    else if(MonthString == "Apr")

       MonthNum = 4;

    else if(MonthString == "May" )

        MonthNum = 5;

    else if(MonthString == "Jun")

        MonthNum =6;

    else if (MonthString == "Jul")

        MonthNum = 7;

    else if (MonthString == "Aug")

        MonthNum = 8;

    else if (MonthString == "Sep")

       MonthNum = 9;

    else if (MonthString == "Oct")

      MonthNum = 10;

    else if(MonthString == "Nov")

       MonthNum = 11;

    else if (MonthString == "Dec")

        MonthNum = 12;
}


Month Month :: nextMonth()
{
    int nextMonth = MonthNum + 1;
    if(MonthNum == 12)
         MonthString = 1;
    return (Month(nextMonth));
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

#ifndef MONTH_H
#define MONTH_H
#include <string>

using namespace std;

class Month
{
    string MonthString;
public:

    Month();
    Month(int MonthNum);
    Month(string MonthString);
    int getMonthNum(){return MonthNum;}
    string getMonthString(){return MonthString;}
    Month nextMonth();


private:
    int MonthNum;
};

#endif // MONTH_H


I don't think this method is implemented correctly.
1
2
3
4
5
6
7
Month Month :: nextMonth()
{
    int nextMonth = MonthNum + 1;
    if(MonthNum == 12)
         MonthString = 1;  //why would the month STRING be 1? Should this be nextMonth = 1?
    return (Month(nextMonth));
}


Also, could you show the code for getMonthString?
In addition to what booradley60 said, have a look at your constructor Month::Month(int NewMonthNum). Nothing in there is setting the month string.

Topic archived. No new replies allowed.