Reputation: 31
I am trying to create a program that will ask for input from the user, a month. Then the program will display the month, the following month, the previous month, the fifth month after and the seventh month before. I can not get the program to subtract a month, or the other following actions. I can only get it to add the next month. Any Thoughts?
#include <iostream>
#include <string>
using namespace std;
enum Month { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC };
Month& operator++(Month& theMonth)
{
if (theMonth == DEC)
theMonth = JAN;
else
theMonth = static_cast<Month>(theMonth + 1);
return theMonth;
}
Month& operator--(Month& theMonth)
{
if (theMonth == JAN)
theMonth = DEC;
else
theMonth = static_cast<Month>(theMonth - 1);
return theMonth;
}
ostream &operator<<(ostream &output, Month &theMonth)
{
switch (theMonth){
case JAN: output << "January";
break;
case FEB: output << "February";
break;
case MAR: output << "March";
break;
case APR: output << "April";
break;
case MAY: output << "May";
break;
case JUN: output << "June";
break;
case JUL: output << "July";
break;
case AUG: output << "Augest";
break;
case SEP: output << "September";
break;
case OCT: output << "October";
break;
case NOV: output << "November";
break;
case DEC: output << "December";
break;
}
return output;
}
istream &operator>>(istream &input, Month &theMonth)
{
int value;
input >> value;
theMonth = static_cast<Month>(value);
return input;
}
#include<iostream>
#include<string>
#include "Month.h"
using namespace std;
int main()
{
Month myMonth;
cout << "Enter Month (1-12): ";
cin >> myMonth;
cout << "You selected month, " << myMonth << " \n" << endl;
cout << "The next month after the month you entered is " << myMonth++ << "\n" << endl;
cout << "The month before the month you entered is " << myMonth-- << "\n" << endl;
cout << "Five months after the month you entered is " << myMonth+= << "\n" << endl;
cout << "Seven months before the month you entered is " << myMonth-= << "\n" << endl;
cin.ignore();
cin.get();
return 0;
}
Upvotes: 3
Views: 58
Reputation: 118435
Instead of using an enumerated value, I would suggest a distinct class, that wraps an int:
class Month {
int month_num; // Month, 0-11. (0-January, 1-February, etc...)
public:
// ...
};
Then, all month operations, such as increment, decrement, add n
months, subtract n
month, etc... -- they all become trivial modulo-12 arithmetic.
Displaying the name of the month also becomes as simple as it possibly can:
static const char * const months[] = {
"January",
"February",
// etc... I'm too lazy to type
"December"
};
So, when implementing your std::ostream
operator<<
overload, guess what "month[month_num]" will do for you?
operator++ becomes:
month_num=(month_num+1) % 12;
operator-- becomes
month_num=(month_num+11) % 12; // Yes, I just can't wrap negative values and modulo inside my brain, this is easier to grok...
operator+=, to add n
to the month number, becomes
month_num=(month_num+n) % 12;
etc...
Upvotes: 4