i need help with this code it compiles and runs fine but it is not giving me the results i expect
the problem is i think on the switch statemen.
Here is where the problem is and then there is the rest of the code

void Month:: Output(ostream& outs)// this fucntion keep getting the default value	 
{
  switch (month)
    {
    case 1:
      outs<< "January" <<endl;
      break;
    case 2:
      outs << "February" <<endl;
      break;
    case 3:
      outs << "March" <<endl;
      break;
    case 4:
      outs << "April" <<endl;
      break;
    case 5:
      outs << "May" <<endl;
      break;
    case 6:
      outs << "June" <<endl;
      break;
    case 7:
      outs << "July" <<endl;
      break;
    case 8:
      outs << "August" <<endl;
      break;
    case 9: 
      outs << "September" <<endl;
      break;
    case 10:
      outs << "October" <<endl;
      break;
    case 11:
      outs << "November" <<endl;
      break;
    case 12:
      outs << "December" <<endl;
      break;
    default:
      outs << "Sorry incorect Month " <<endl;
    }
#include <iostream>
using namespace std;
 
class Month
{
public:
  Month (char char1, char char2, char  char3);
  // Precondition: The parameters contain the first three characters
  // in the name of the month - all lowercase.
  // Postcondition: The member variable month has been set to
  // the integer value corresponding to the letters (1 for jan, 
  // (2 for feb, and so on). month is set to 0 and a message is
  // printed if the letters do not correspond to a valid month.
 
  Month (int monthNum);
  // Precondition: The parameter monthNum contains a valid 
  // month number (1 - 12)
  // Postcondition: The member variable month has been set to 
  // the value of the parameter monthNum.
 
  Month();
  // Sets the member variable month to 1 (defaults to January).
 
  void Input (istream& in);
  // Postcondition: The member variable month has been output
  // to the screen if it is valid; otherwise a "not valid"
  // message is printed.
 
  void Output(ostream& outs);
  // Postcondition: The first three letters of the name of the
  // month has been output to the screen if the month is
  // valid (1 - 12); otherwise a message indicating the month
  // is not valid is output
  Month Next();

 
private:
  int month;
  
};
 
 
 void main ()
{
	 
	 Month m1;
	
	
	 cout<<" enter month"<< endl;

	 m1.Input(cin);
	 
	 m1.Output(cout);     
}

 Month::Month(): month(0)
{
	 

}
  void	Month::Input(istream& in)
{
  char first,second,third;
  int number_month;
  char c;
  
  c=in.peek();

  if( (c > '0') && (c <= '9'))
		{
		in >> number_month;
		Month(nuber_month);
		
		}
  
	 else
	  {
		in >>first>>second>>third;
		Month(first,second,third);
		
		
	  }

  }
Month::Month(char char1, char char2, char char3)
{	
    
     if( (char1=='j'|| char1 =='J') && (char2=='a'|| char2=='A')&&(char3=='n'|| char3=='N'))
		{	month=1;
		cout<< month;
		Month(month);}
    if(( char1=='f' || char1=='F') && (char2 == 'e' || char2 == 'E') && ( char3 == 'b'|| char3=='B'))
		{month=2;}
     if(( char1=='m' || char1=='M') && (char2 == 'a' || char2 == 'A') && ( char3 == 'r'|| char3=='R'))
		{month=3;
		cout<< month;}
     if(( char1=='a' || char1=='A') && (char2 == 'p' || char2 == 'P') && ( char3 == 'r'|| char3=='R'))
		{month=4;
		cout<< month;}
     if(( char1=='m' || char1=='M') && (char2 == 'a' || char2 == 'A') && ( char3 == 'y'|| char3=='Y'))
		{month=5;
		cout<< month;}
    if(( char1=='j' || char1=='J') && (char2 == 'u' || char2 == 'U') && ( char3 == 'n'|| char3=='N'))
		{month=6;
		cout<< month;}

     if(( char1=='j' || char1=='J') && (char2 == 'u' || char2 == 'U') && ( char3 == 'l'|| char3=='L'))
		{month=7;
		}
     if(( char1=='a' || char1=='A') && (char2 == 'u' || char2 == 'U') && ( char3 == 'g'|| char3=='G'))
		{month=8;
		}
     if(( char1=='s' || char1=='S') && (char2 == 'e' || char2 == 'E') && ( char3 == 'p'|| char3=='P'))
		{month=9;
		cout<< month;}

     if(( char1=='o' || char1=='O') && (char2 == 'c' || char2 == 'C') && ( char3 == 't'|| char3=='T'))
		{month= 10;}
		
    if((char1=='n' || char1=='N') && (char2 == 'o' || char2 == 'O') && ( char3 == 'v'|| char3=='V'))
		{month=11;}
    if(( char1=='d' || char1=='D') && (char2 == 'e' || char2 == 'E') && ( char3 == 'c'|| char3=='C'))
		{month=12;}
	
   cout<<endl<<char1<<char2<<char3;// this is to test it is taking the right chars from input
		
Month(month);
}
 Month::Month(int monthNum)
{
	 
	if (monthNum>0 && monthNum<=12)
		
	month=monthNum;
	else
	{
	}
		
}

/*here is the proble it is taking the default value to do the switch and not the value that month just got in the previous function. why is it doing that or is that i am using the wrong concepts???*/

void Month:: Output(ostream& outs)
	 
{
  switch (month)
    {
    case 1:
      outs<< "January" <<endl;
      break;
    case 2:
      outs << "February" <<endl;
      break;
    case 3:
      outs << "March" <<endl;
      break;
    case 4:
      outs << "April" <<endl;
      break;
    case 5:
      outs << "May" <<endl;
      break;
    case 6:
      outs << "June" <<endl;
      break;
    case 7:
      outs << "July" <<endl;
      break;
    case 8:
      outs << "August" <<endl;
      break;
    case 9: 
      outs << "September" <<endl;
      break;
    case 10:
      outs << "October" <<endl;
      break;
    case 11:
      outs << "November" <<endl;
      break;
    case 12:
      outs << "December" <<endl;
      break;
    default:
      outs << "Sorry incorect Month " <<endl;
    }
  
}


	Month  Month :: Next()
{
		int next;
	if (month==12)
		next=1;
	else
	{
		 next=month++;
	}
	cout<< next;
	return( Month(next));
}

Recommended Answers

All 4 Replies

lines 72, 79 and 127. constructors can't be called directly like that. You have to instantiate an object of type Month in order to call the constructor. One way to fix the problem is to create a set function.

class Month
{
 public:
    ...
    void set(int month)
    {
         if(month > 0 && month <= 12)
             this->month = month;
     }
     ...
}

Now your program can call SetMonth() whenever it needs to instead of calling the class constructor.

i thought i had the samething on lines 128-140 i changed it like u have it plus how am i suppose to call it i guess i am lost now

remove line 127.

In main() you declare an instance of class Month and call it m1. You then call two member functions correctly a few lines later.

You could try creating two other instances using your 2 other contructors and output the results if you want.

Month m1('J', 'u', 'l');
Month m3(9);

If you use Ancient Dragon's set function then you could change any Months value like this:

m1.set(12);
or
int i = 2;
m2.set(i);

1. Avoid these horrible switch cascades (as in Month::Output) where possible (it's that case).
2. Don't add unnecessary dependencies in your class design. For example, your Month class is dependent of ostreams via Month::Output(). Why? Better add some useful functionalities (and see point #1 above):

const char* Month:: Name() const {
    static const char* name[] = {
        "\?",
        "January","February","March",
        "April", "May", "June",
        "July", "August", "September",
        "October", "November", "December"
    };
    return name[month < 1 || month > 12? 0: month];
}

No need in Month::Output(ostream&) now, everybody can write os << m.Name() or what else...

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.