Make your own free website on Tripod.com
« May 2018 »
S M T W T F S
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
You are not logged in. Log in
Entries by Topic
All topics  «
Blog Tools
Edit your Blog
Build a Blog
RSS Feed
View Profile
Month Class
Thursday, 3 April 2008
Month Class

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#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;
    usermonth++;
       cout << "   After incrementing the month:" << endl;
    cout<<"   " << usermonth << endl;
    --usermonth;
       cout << "   After decrementing the month:" << endl;
    --usermonth;
    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);
}


Posted by veronicak5678 at 4:49 PM EDT

Newer | Latest | Older