Problem using class increment and decrement operators

First let me start by saying that my code was too long for this post so down inbetween where it says >>START and <<END is where I put my increment and decrement operator functions. I will post them in the first comment. They were VERY long. At this moment I can not get my int main to let me use my doy.print() function twice. It works for the first print but then does not for the second. I will need to fix this first before I can even see if I have problems with my increment and decrement functions. This program takes an int day and string month and checks to see if the day is out of range for how many days are in said month. It will output an error message if incorrect or output the date in month-day format if correct. I had to add the increment and decrement operators for this second half of the problem. It can be found in chapter 11 program problems in the "Starting out with C++ Early Objects" book.

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <string>
#include <iostream>

using namespace std;

class DayOfYear
{
   private:
      
      static int day;//stores the integer input by user into class
      static string month;//stores the message for date output
      
   public:

      DayOfYear& operator++();//increment operator prefix
      DayOfYear& operator--();//decrement operator prefix      
      
      DayOfYear operator++(int);//increment operator postfix
      DayOfYear operator--(int);//decrement operator postfix
      
    
            
      DayOfYear(string classMonth, int classDay)//constructor
      {
         day = classDay;
         month = classMonth;
         
         if(day >= 1 && day <= 31)
         {
            if(day > 28)
            {
               if(month == "FEBRUARY")
               //won't allow february to have a 29th,30th, or 31st
               {
                 month = "NOTHIN";
               }
               else if(day > 30)
               {
                  if(month == "APRIL")
                  {
                     month = "NOTHIN";
                  }
                  else if(month == "JUNE")
                  {
                     month = "NOTHIN";
                  }
                  else if(month == "SEPTEMBER")
                  {
                     month = "NOTHIN";
                  }
                  else if(month == "NOVEMBER")
                  {
                     month = "NOTHIN";
                  }
               }
                  
              }

           }
           else
           {
            month = "NOTHIN";
           }
      }
  
  void print()//function that outputs the string in month-day format
  {
               
     if(month == "NOTHIN")//checks to see if the constructor received bad input
     {
      cout << "INCORRECT INPUT";
     }
     if(month == "JANUARY")//checks to see if day is within january
     {
        month = "January";
        cout << month << " " << day << "\n";
     }
     else if(month == "FEBRUARY")
     {
        month = "February";
        cout << month << " " << day << "\n";
     }
     else if(month == "MARCH")
     {
        month = "March";
        cout << month << " " << day << "\n";
     }
     else if(month == "APRIL")
     {
        month = "April";
        cout << month << " " << day << "\n";
     }
     else if(month == "MAY")
     {
        month = "May";
        cout << month << " " << day << "\n";
     }
     else if(month == "JUNE")
     {
        month = "June";
        cout << month << " " << day << "\n";
     }
     else if(month == "JULY")
     {
        month = "July";
        cout << month << " " << day << "\n";
     }
     else if(month == "AUGUST")
     {
        month = "August";
        cout << month << " " << day << "\n";

     }
     else if(month == "SEPTEMBER")
     {
        month = "September";
        cout << month << " " << day << "\n";
     }
     else if(month == "OCTOBER")
     {
        month = "October";
        cout << month << " " << day << "\n";

     }
     else if(month == "NOVEMBER")
     {
        month = "November";
        cout << month << " " << day << "\n";
     }
     else if(month == "DECEMBER")
     {
        month = "December";
        cout << month << " " << day << "\n";
     }
  }
     

};

>>START

<<END
   
   
string DayOfYear::month = "";//declares the static string
int DayOfYear::day = 0;//declares the static int    


int main()
{
   string monthInput;//used to pass the month to the class
   int dayInput;//used to pass day to the class
   cout << "Enter an integer from 1 to 31 for the month: ";
   cin >> dayInput;
   cout << "Enter a string for a month all uppercase: ";
   cin >> monthInput;
   DayOfYear doy(monthInput, dayInput);//creates a DayOfYear object in int main
   doy.print();//outputs the month and day format
   cout << "\nHere is the post incremented date: ";//formatting purposes
   doy.print();
   cout << "\n";
   return 0;
}
   
      
You are modifying month inside the print() function so next time you call the function it will no longer be all uppercase.
I fixed that problem and now my output works except for prefix increment and decrement operators. I assume the same problems are on the postfix also but they won't even output til after. If I put in 31 for int and JANUARY for string, I get that the output for the day is 32 and month is JANUARY.
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <string>
#include <iostream>

using namespace std;

class DayOfYear
{
   private:
      
      static int day;//stores the integer input by user into class
      static string month;//stores the message for date output
      
   public:

      DayOfYear& operator++();//increment operator prefix
      DayOfYear& operator--();//decrement operator prefix      
      
      DayOfYear operator++(int);//increment operator postfix
      DayOfYear operator--(int);//decrement operator postfix
      
    
            
      DayOfYear(string classMonth, int classDay)//constructor
      {
         day = classDay;
         month = classMonth;
         
         if(day >= 1 && day <= 31)
         {
            if(day > 28)
            {
               if(month == "FEBRUARY")
               //won't allow february to have a 29th,30th, or 31st
               {
                 month = "NOTHIN";
               }
               else if(day > 30)
               {
                  if(month == "APRIL")
                  {
                     month = "NOTHIN";
                  }
                  else if(month == "JUNE")
                  {
                     month = "NOTHIN";
                  }
                  else if(month == "SEPTEMBER")
                  {
                     month = "NOTHIN";
                  }
                  else if(month == "NOVEMBER")
                  {
                     month = "NOTHIN";
                  }
               }
                  
              }

           }
           else
           {
            month = "NOTHIN";
           }
      }
  
  void print()//function that outputs the string in month-day format
  {
               
     if(month == "NOTHIN")//checks to see if the constructor received bad input
     {
      cout << "INCORRECT INPUT";
     }
     if(month == "JANUARY")//checks to see if day is within january
     {
        cout << month << " " << day << "\n";
     }
     else if(month == "FEBRUARY")
     {
        cout << month << " " << day << "\n";
     }
     else if(month == "MARCH")
     {
        cout << month << " " << day << "\n";
     }
     else if(month == "APRIL")
     {
        cout << month << " " << day << "\n";
     }
     else if(month == "MAY")
     {
        cout << month << " " << day << "\n";
     }
     else if(month == "JUNE")
     {
        cout << month << " " << day << "\n";
     }
     else if(month == "JULY")
     {
        cout << month << " " << day << "\n";
     }
     else if(month == "AUGUST")
     {
        cout << month << " " << day << "\n";

     }
     else if(month == "SEPTEMBER")
     {
        cout << month << " " << day << "\n";
     }
     else if(month == "OCTOBER")
     {
        cout << month << " " << day << "\n";

     }
     else if(month == "NOVEMBER")
     {
        cout << month << " " << day << "\n";
     }
     else if(month == "DECEMBER")
     {
        cout << month << " " << day << "\n";
     }
  }
     

};

>>START

<<END
   
   
string DayOfYear::month = "";//declares the static string
int DayOfYear::day = 0;//declares the static int    


int main()
{
   string monthInput;//used to pass the month to the class
   int dayInput;//used to pass day to the class
   cout << "Enter an integer from 1 to 31 for the month: ";
   cin >> dayInput;
   cout << "Enter a string for a month all uppercase: ";
   cin >> monthInput;
   DayOfYear doy(monthInput, dayInput);//creates a DayOfYear object in int main
   doy.print();//outputs the month and day format
   cout << "\nHere is the post incremented date: ";
   (doy++).print();
   cout << "\n";
   cout << "Here is the post decremented date: ";
   (doy--).print();
   cout << "\n";
   cout << "Here is the pre incremented date: ";
   (++doy).print();
   cout << "\n";
   cout << "Here is the pre decremented date: ";
   (--doy).print();
   cout << "\n";
   return 0;
}

Last edited on
and here are my functions for the increment and decrement postfix and prefix. remember that they are inserted between >>START and <<END.
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
DayOfYear& DayOfYear::operator++()
  {
    ++day;
    if(day > 29)
    {
       if(month == "FEBRUARY")
       {
          day = 1;
          month = "MARCH";
       }
       else if (day > 30)
       {
          if(month == "APRIL")
          {
             day = 1;
             month = "MAY";
          }
          else if(month == "JUNE")
          {
             day = 1;
             month = "JULY";
          }
          else if(month == "SEPTEMBER")
          {
             day = 1;
             month = "OCTOBER";
          }
          else if(month == "NOVEMBER")
          {
             day = 1;
             month = "DECEMBER";
          }
       }
       else if(day == 31)
       {
          if(month == "JANUARY")
          {
             day = 1;
             month = "FEBRUARY";
          }
          else if(month == "MARCH")
          {
             day = 1;
             month = "APRIL";
          }
          else if(month == "MAY")
          {
             day = 1;
             month = "JUNE";
          }
             else if(month == "JULY")
          {
             day = 1;
             month = "AUGUST";
          }
          else if(month == "AUGUST")
          {
             day = 1;
             month = "SEPTEMBER";
          }
          else if(month == "OCTOBER")
          {
             day = 1;
             month = "NOVEMBER";
          }
          else if(month == "DECEMBER")
          {
             day = 1;
             month = "JANUARY";
          }
        }
     }
}  
   
DayOfYear DayOfYear::operator++(int)
{
    int temp = day;
    temp++;
    if(temp > 29)
    {
       if(month == "FEBRUARY")
       {
          temp = 1;
          month = "MARCH";
       }
       else if (temp > 30)
       {
          if(month == "APRIL")
          {
             temp = 1;
             month = "MAY";
          }
          else if(month == "JUNE")
          {
             temp = 1;
             month = "JULY";
          }
          else if(month == "SEPTEMBER")
          {
             temp = 1;
             month = "OCTOBER";
          }
          else if(month == "NOVEMBER")
          {
             temp = 1;
             month = "DECEMBER";
          }
       }
       else if(temp == 31)
       {
          if(month == "JANUARY")
          {
             temp = 1;
             month = "FEBRUARY";
          }
          else if(month == "MARCH")
          {
             temp = 1;
             month = "APRIL";
          }
          else if(month == "MAY")
          {
             temp = 1;
             month = "JUNE";
          }
          else if(month == "JULY")
          {
             temp = 1;
             month = "AUGUST";
          }
          else if(month == "AUGUST")
          {
             temp = 1;
             month = "SEPTEMBER";
          }
          else if(month == "OCTOBER")
          {
             temp = 1;
             month = "NOVEMBER";
          }
          else if(month == "DECEMBER")
          {
             temp = 1;
             month = "JANUARY";
          }
        }
     }
}  
   
DayOfYear& DayOfYear::operator--()
{
    --day;
    if(day == 0)
    {
       if(month == "JANUARY")
       {
          day = 31;
          month = "DECEMBER";
       }
       else if(month == "FEBRUARY")
       {
          day = 31;
          month = "JANUARY";
       }
       else if(month == "MARCH")
       {
          day = 29;
          month = "FEBRUARY";
       }
       else if(month == "APRIL")
       {
          day = 31;
          month = "MARCH";
       }
       else if(month == "MAY")
       {
          day = 30;
          month = "APRIL";
       }
       else if(month == "JUNE")
       {
          day = 31;
          month = "MAY";
       }
       else if(month == "JULY")
       {
          day = 30;
          month = "JUNE";
       }
       else if(month == "AUGUST")
       {
          day = 31;
          month = "JULY";
       }
       else if(month == "SEPTEMBER")
       {
          day = 31;
          month = "JULY";
       }
       else if(month == "OCTOBER")
       {
          day = 30;
          month = "SEPTEMBER";
       }
       else if(month == "NOVEMBER")
       {
          day = 31;
          month = "OCTOBER";
       }
       else if(month == "DECEMBER")
       {
          day = 30;
          month = "NOVEMBER";
       }
    }
}  
   
DayOfYear DayOfYear::operator--(int)
{
    int temp = day;
    temp--;
    if(temp == 0)
    {
      if(month == "JANUARY")
       {
          temp = 31;
          month = "DECEMBER";
       }
       else if(month == "FEBRUARY")
       {
          temp = 31;
          month = "JANUARY";
       }
       else if(month == "MARCH")
       {
          temp = 29;
          month = "FEBRUARY";
       }
       else if(month == "APRIL")
       {
          temp = 31;
          month = "MARCH";
       }
       else if(month == "MAY")
       {
          temp = 30;
          month = "APRIL";
       }
       else if(month == "JUNE")
       {
          temp = 31;
          month = "MAY";
       }
       else if(month == "JULY")
       {
          temp = 30;
          month = "JUNE";
       }
       else if(month == "AUGUST")
       {
          temp = 31;
          month = "JULY";
       }
       else if(month == "SEPTEMBER")
       {
          temp = 31;
          month = "JULY";
       }
       else if(month == "OCTOBER")
       {
          temp = 30;
          month = "SEPTEMBER";
       }
       else if(month == "NOVEMBER")
       {
          temp = 31;
          month = "OCTOBER";
       }
       else if(month == "DECEMBER")
       {
          temp = 30;
          month = "NOVEMBER";
       }
    }
} 
also lot of else ifs you should maybe make an enum called months and put this inside a switch statement

would make the code tidier
Yeah I realized that afterwards but this is what I got now. Probs would have saved me like 200 lines of code... Do you have any idea what's wrong with the increment or decrement operators though?
1
2
3
4
5
6
7
8
else if (temp > 30)
{
	code1...
}
else if (temp == 31)
{
	code2...
}

code1 will run instead code2 when temp == 31 because 31 > 30.
I concatenated that second else if into the one with the expression (temp > 30) on both the prefix and postfix but I still get the same output where it doesn't change the month and the decrement operator doesn't do anything at all. Here is the output:
//
./a.out
Enter an integer from 1 to 31 for the month: 31
Enter a string for a month all uppercase: JANUARY
JANUARY 31

Here is the post incremented date: JANUARY 31

Here is the post decremented date: JANUARY 31

Here is the pre incremented date: JANUARY 32

Here is the pre decremented date: JANUARY 31
//

I thought you were right about what you pointed out but it seems that concatenation didn't work and I honestly haven't a clue about the decrement operator. It should subtract 1 from day and then check to see it isn't 0. It doesn't even subtract one though??
You've forgot to add a return statement to each of these operators. The compiler should be able to warn you about simple things like that. If you use GCC you should at least use the -Wall compiler flag.
Seems the pattern going around is to implement pre- operators and then call them from the post- , making the post- ones quite short.
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <string>
#include <iostream>
#include <vector>

using namespace std;

class Month
{
public:
    enum MonthEnum 
    {   
        JANUARY=1, FEBRUARY, MARCH, APRIL, MAY, JUNE, 
        JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER
    };
    Month() : month_(MonthEnum::JANUARY)
    {
    }
    
    Month(MonthEnum m) : month_(m)
    {
    }
    
    Month(int m) : month_((MonthEnum)m)
    {
    }
    
    bool operator==(MonthEnum m)
    {
        return month_ == m;
    }
    
    Month& operator++() // Pre
    {
        if (month_ == MonthEnum::DECEMBER)
            month_ = MonthEnum::JANUARY;
        else
            month_ = (MonthEnum)((int)month_ + 1);
            
        return *this;
    }
    
    Month operator++(int) // Post
    {
        Month tmp(*this);
        operator++();
        return tmp;
    }
    
    Month& operator--() // Pre
    {
        if (month_ == MonthEnum::JANUARY)
            month_ = MonthEnum::DECEMBER;
        else
            month_ = (MonthEnum)((int)month_ - 1);
            
        return *this;
    }
    
    Month operator--(int) // Post
    {
        Month tmp(*this);
        operator--();
        return tmp;
    }
    
    int Numeric() const
    {
        return (int)month_;
    }
    string str() const
    {
        return strings_[(int)month_-1];
    }
    
private:
    MonthEnum month_;
    static const vector<string> strings_;
};
const vector<string> Month::strings_ = {"January", "February", "March", "April",
"May", "June", "July", "August", "September", "October", "November", "December"};

ostream& operator<<(ostream& os, const Month& m)
{
    os << m.str();
    return os;
}


class DayOfYear
{


public:
    DayOfYear& operator++()
    {
        day_++;
        if(day_ == 29 && month_ == Month::MonthEnum::FEBRUARY)
        {
           day_ = 1;
           month_ == Month::MonthEnum::MARCH;
        }
        else if (day_ == 31)
        {
            if(month_ == Month::MonthEnum::APRIL || 
               month_ == Month::MonthEnum::JUNE || 
               month_ == Month::MonthEnum::SEPTEMBER || 
               month_ == Month::MonthEnum::NOVEMBER)
            {
                day_ = 1;
                month_++;
            }
        }
        else if (day_ == 32)
        {
            day_ = 1;
            month_++;
        }
        return *this;
    }
    
    DayOfYear operator++(int)
    {
        DayOfYear tmp(*this);
        operator++();
        return tmp;
    }

    DayOfYear& operator--()
    {
        day_--;
        if(day_ == 0)
        {
            if (month_ == Month::MonthEnum::MARCH)
            {
                day_ = 28;
                month_--;
            }
            else if(month_ == Month::MonthEnum::MAY || 
               month_ == Month::MonthEnum::JULY || 
               month_ == Month::MonthEnum::OCTOBER || 
               month_ == Month::MonthEnum::DECEMBER)
            {
                day_ = 30;
                month_--;
            }
            else
            {
                day_ = 31;
                month_--;
            }
        }
        return *this;
    }
    
    DayOfYear operator--(int)
    {
        DayOfYear tmp(*this);
        operator--();
        return tmp;
    }
    
    DayOfYear(Month month, int day) : month_(month), day_(day)
    {
    }
    
    void Show()
    {
        cout << month_ << " " << day_ << endl;
    }

private:
    Month month_;
    unsigned int day_;

};

int main()
{
   DayOfYear doy(Month::MonthEnum::APRIL, 30);
   doy.Show();
   
   doy++;
   cout << "\nHere is the post-incremented date: " << endl;
   doy.Show();
   
   cout << "\nAnother date:" << endl;
   DayOfYear doy2(Month::MonthEnum::DECEMBER, 1);
   doy2.Show();
   --doy2;
   cout << "Decremented:"<< endl;
   doy2.Show();
   

   cout << endl;
   return 0;
}

Output, running at https://repl.it/repls/LimpZigzagBinarytree
April 30

Here is the post-incremented date: 
May 1

Another date:
December 1
Decremented:
November 30
Last edited on
Topic archived. No new replies allowed.