homework help - overloaded constructor taking char or int
The whole point of the homework is to test an overloaded constructor. If I manually put an int into the constructor it works and I get the correct result BUT I cannot figure out how to get it to take a set of 3 characters like "jun" or "dec".
If I leave the constructor empty, it runs the default constructor like it should.
If I put in a "2", February comes out like it should.
But if I put in "jan" or any other set of 3 characters I get a blank.
Code:
#include <iostream>
using namespace std;
class month
{
private:
int mnth;
public:
month(); //default constructor
month(char newMonth); //overloaded constructor
month(int newMonthInt); //overloaded constructor
int getMonth(); //accessor
void setMonth(int userMonth); //mutator
void displayMonth();
};
void main ()
{
month test;
test = month(); //enter month here by int or 3char
test.displayMonth();
}
void month::setMonth(int userMonth)
{
mnth = userMonth;
}
int month::getMonth()
{
return mnth;
}
month::month()
{
mnth = 1;
}
month::month(int userMonthInt)
{
mnth = userMonthInt;
}
month::month(char newMonth)
{
if (newMonth == 'jan')
mnth = 1;
else if (newMonth == 'feb')
mnth = 2;
else if (newMonth == 'mar')
mnth = 3;
else if (newMonth == 'apr')
mnth = 4;
else if (newMonth == 'may')
mnth = 5;
else if (newMonth == 'jun')
mnth = 6;
else if (newMonth == 'jul')
mnth = 7;
else if (newMonth =='aug')
mnth = 8;
else if (newMonth == 'sep')
mnth = 9;
else if (newMonth == 'oct')
mnth = 10;
else if (newMonth == 'nov')
mnth = 11;
else if (newMonth == 'dec')
mnth = 12;
}
void month::displayMonth()
{
if (mnth == 1)
cout << "January";
else if (mnth == 2)
cout << "February";
else if (mnth == 3)
cout << "March";
else if (mnth == 4)
cout << "April";
else if (mnth == 5)
cout << "May";
else if (mnth == 6)
cout << "June";
else if (mnth == 7)
cout << "July";
else if (mnth == 8)
cout << "August";
else if (mnth == 9)
cout << "September";
else if (mnth == 10)
cout << "October";
else if (mnth == 11)
cout << "November";
else if (mnth == 12)
cout << "December";
cout << " was entered." << endl;
}
Thanks in advance for any advice or help.