Problem with Class Month

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.

Here is the source code.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <iostream>

using namespace std;

class Month
{
	public:
		Month (char firstLetter, char secondLetter, char thirdLetter);
		//Set the name of the month to the first 3 chars of its name
	
		Month (int month);
		//Set the months to their respective numbers

		Month ();
		//Initialize the Month name to "", and the month # to 0

		void Input (istream& ins);
		//reads either the month name or the month number

		void Output(ostream& outs);
		//Prints the full name of the month

		Month next();
		//returns the next month as a value of type Month.


	private:
		int Month_Number;
	};

int main ()
	{
	//Month m1(6);	
	//Month m2('F','e','b');
	Month m3;

	//m1.Output (cout);
	//m2.Output(cout);
	cout << "Please enter month number or name: ";
	m3.Input(cin);
	m3.Output(cout);

	return 0;
	}

Month::Month() : Month_Number(0){}

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;
		else 
			ins >> firstLetter >> secondLetter >> thirdLetter;
	}


Month::Month(char firstLetter, char secondLetter, char thirdLetter)
	{
	//Make all char lowercase
	firstLetter = tolower(firstLetter);
	secondLetter = tolower(secondLetter);
	thirdLetter = tolower(thirdLetter);
	
	if ((firstLetter == 'j')&&(secondLetter == 'a')&&(thirdLetter == 'n'))
		Month_Number = 1;
	else if ((firstLetter == 'f')&&(secondLetter == 'e')&&(thirdLetter == 'b'))
		Month_Number = 2;
	else if ((firstLetter == 'm')&&(secondLetter == 'a')&&(thirdLetter == 'r'))
		Month_Number = 3;
	else if ((firstLetter = 'a')&&(secondLetter == 'p')&&(thirdLetter == 'r'))
		Month_Number = 4;
	else if ((firstLetter == 'm')&&(secondLetter == 'a')&&(thirdLetter == 'y'))
	   Month_Number = 5;
	else if ((firstLetter == 'j')&&(secondLetter == 'u')&&(thirdLetter == 'n'))
		Month_Number = 6;
	else if ((firstLetter == 'j')&&(secondLetter == 'u')&&(thirdLetter == 'l'))
		Month_Number = 7;
	else if ((firstLetter == 'a')&&(secondLetter == 'u')&&(thirdLetter == 'g'))
		Month_Number = 8;
	else if ((firstLetter == 's')&&(secondLetter == 'e')&&(thirdLetter == 'p'))
		Month_Number = 9;
	else if ((firstLetter == 'o')&&(secondLetter == 'c')&&(thirdLetter == 't'))
		Month_Number = 10;
	else if ((firstLetter == 'n')&&(secondLetter == 'o')&&(thirdLetter == 'v'))
		Month_Number = 11;
	else if ((firstLetter == 'd')&&(secondLetter == 'e')&&(thirdLetter == 'c'))
		Month_Number = 12;
	else 
		cout << "Error - This month is not a valid!" << endl;
			exit(1);
	}

Month::Month (int month)
	{
		if ((month < 1) || (month > 12))
		{
			cout << "Error - This month is not a valid!" << endl;
			exit(1);
		}
		else 
			Month_Number = month;

	}
void Month::Output(ostream& outs)
{

  switch (Month_Number)
    {
    case 1:
      outs << "January";
      break;
    case 2:
      outs << "February"; 
      break;
    case 3:
      outs << "March";
      break;
    case 4:
      outs << "April";
      break;
    case 5:
      outs << "May";
      break;
    case 6:
      outs << "June";
      break;
    case 7:
      outs << "July";
      break;
    case 8:
      outs << "August";
      break;
    case 9: 
      outs << "September";
      break;
    case 10:
      outs << "Octuber";
      break;
    case 11:
      outs << "November";
      break;
    case 12:
      outs << "December";
      break;
    default:
      outs << "Error - This month is not a valid!" << endl;
    }

  cout << "\n";
}

Month Month::next()	
	{	
	int Num;

		if (Month_Number == 12)
			Num = 1;
		else
			Num = Month_Number + 1;

	return(Month (Num));


	}
Last edited on
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);}
	}


Also there was a typing problem in the nested if:

1
2
else if ((firstLetter = 'a')&&(secondLetter == 'p')&&(thirdLetter == 'r'))
		Month_Number = 4;

instead of
1
2
else if ((firstLetter == 'a')&&(secondLetter == 'p')&&(thirdLetter == 'r'))
		Month_Number = 4;

Thank you for being the only one who reply to my post.
Topic archived. No new replies allowed.