Class Increment and Decrement operators not working?
Apr 11, 2018 at 12:34am UTC
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. My problem is that the increment and decrement post and prefix operators seem to not be working correctly. The increment will increase the day by 1 but not cycle to the next month if the day goes out of bounds and the decrement seems to do nothing. Maybe plug em in and see what you get so you understand what I'm saying?
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 Apr 11, 2018 at 12:34am UTC
Apr 11, 2018 at 12:35am UTC
Here are the increment and decrement operators that go 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" ;
}
}
}
Topic archived. No new replies allowed.