Hello guys,
I need help with my C++ Program. I am writing a class called Month with a couple of constructor which will determine that the month name is valid, set the Month_name to the first tree letters of its name, and set the right month name it its corresponding number. Also, there are two functions: input which test whether what the user enters was a char or an int. And , an output function that will display the full name of the month. Also another function that will display the next month. A main function will test the class.
The problems are the following. The Constructor that is setting the month name to its corresponding number require a series of if and else that is giving me problems n' I don't know how to solve them. Also, the input function is not assigning the int or chars to the right function. When I test it, it always give me the bad case of the test. Error! - this months is not valid.
Your Input method does not assign Month_Number a value, so it is always 0.
Your lengthy constructor always exits since the exit(0) is NOT inside the else case.
To shorten that constructor you can use a lookup table. Make an array of three letter month names and search the array for the inputted characters. The index into the array at which you find the match is the month number (you may need to add 1).
Thanks a lot Jsmith. What I did to solve the problems was to use the this pointer in order to assign the value I got from the input.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void Month::Input(istream& ins)
{
char c, firstLetter, secondLetter, thirdLetter ;
int i;
c = ins.peek();
//Deternime whether c is an integer of a character
if ( (c >= '1') && (c <= '9') )
{ins >> i;
this = Month(i);}
else
{ins >> firstLetter >> secondLetter >> thirdLetter;
this = Month(firstLetter, secondLetter, thirdLetter);}
}