I am trying to write a program with a Month class that will take a month as input, then display the previous and next month. It should use overloaded prefix+++ and -- operators and postfix ++ and -- operators. My month is decrementing, but when it should display the incremented month, it shows the same month inputted. I cant see why. Here's the code:

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <istream>
#include <ostream>
#include <fstream>
using namespace std;


class Month
{
      private:
	          string name;
	          int monthNumber;
	          string getName(int tempNumber)
	          {
		             switch(tempNumber)
		             {
		                            case 1: return "January";
		                            case 2: return "February";
		                            case 3: return "March";
		                            case 4: return "April";
		                            case 5: return "May";
		                            case 6: return "June";
		                            case 7: return "July";
		                            case 8: return "August";
		                            case 9: return "September";
		                            case 10: return "October";
            		                case 11: return "November";
		                            case 12: return "December";
		                            default: return "Invalid month number";
                     }
               }
               int getNumber(string tempName)
	           {
                     if(tempName == "January" ) return 1;
		             if(tempName == "February" ) return 2;
		             if(tempName == "March" ) return 3;
		             if(tempName == "April") return 4;
		             if(tempName == "May" ) return 5;
		             if(tempName == "June" ) return 6;
		             if(tempName == "July" ) return 7;
		             if(tempName == "August" ) return 8;
		             if(tempName == "September" ) return 9;
		             if(tempName == "October" ) return 10;
		             if(tempName == "November") return 11;
		             if(tempName == "December" ) return 12;
		             else
		             {
		                 cout << "\n   Error!!" << endl;
		                 cout << "   The program will now terminate" << endl;
		                 return 0; 
                     }
               }
      public:
	          Month()
	          {
		             name = "January";
		             monthNumber= 1;
                           }
       	          Month(string tempName)
                          {
                                             name = tempName;
		             monthNumber = getNumber(tempName);
                          }
                          string getMonth()
	          {
		             return name;
                          }
	          int getNumber()
	          {
                                           return monthNumber;
                            }
                            friend ostream &operator << (ostream &out, Month &temp);
	            friend istream &operator >> (istream &in, Month &temp);
          	            Month &operator++()
                            {
                                           if(monthNumber == 12)
		             {
			                     monthNumber = 1;
			                     name = getName(monthNumber);
			                     return *this;
                                              }
		             else
		             {
			                     ++monthNumber;
			                     name = getName(monthNumber);
			                     return *this;
                                             }
                          }
	          Month &operator++(int)
	          {
                                            Month temp(name);
		            if(monthNumber== 12)
		            {
                                                                     monthNumber = 1;
			                     name = getName(monthNumber);
                                             }
		            else
		            {
			                     monthNumber++;
			                     name = getName(monthNumber);
                                             }
		            return temp;
                           }
	          Month &operator--()
	          {
		            if(monthNumber == 1)
		            {
                                                                     monthNumber = 12;
		 	                     name = getName(monthNumber);
			                     return *this;
                                             }
		            else
		            {
                                                                     --monthNumber;
			                     name = getName(monthNumber);
			                     return *this;
                                             }
                       }
                      Month &operator--(int)
                       {
                                          Month temp(name);
                                          monthNumber--;
                                          name = getName(monthNumber);
                                         return temp;
                         }
};


istream &operator >> (istream &in, Month &temp)
{
        cout << "Please Enter Month: ";
        in >> temp.name;
        temp.monthNumber= temp.getNumber(temp.name);
        return in;
}
ostream &operator<<(ostream &out, Month &temp)
{
        out << temp.name << endl;
        return out;
}

void Clear_Screen(void);
void Flush(void);

int main()
{
	char choice;
	do
	{
	   Clear_Screen();
	   Month usermonth;
	   cin >> usermonth;
	   cout << usermonth << endl << "   After decrementing the Month:" << endl;
	   --usermonth;
	   cout<<"   " << usermonth << endl;
	   usermonth++;
       cout << "   After incrementing the Month:" << endl;
	   cout<<"   " << usermonth << endl;
	   cout << "   Would you like to enter another month? (Y/N): ";
	   cin >> choice;
	}
   while(choice == 'y' || choice == 'Y');
   cout << "\n\n   Thank you for using the program!" << endl;
   Flush();
   cout << "\n\n   Press any key to exit" << endl;
   getchar();
   return 0;
}


void Clear_Screen(void)
{
   #ifdef _WIN32
      system("cls");
   #else
      system("clear");
   #endif
}


void Flush(void)
{
   int ch;
   do
   {
      ch = getchar();
   }
   while (ch != EOF && ch != '\n');
   clearerr(stdin);
}

Sorry the code is so messy. I tried to format it, but it keeps showing up like this.

Never mind! That was a stupid mistake. I didn't think that after I incremented it, it was one month ahead. Argh!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.