How to properly overload >> input operator?

I am trying to overload the >> operator. I want to use my object called testcase1 and initialize it with the overloaded >> operator to set the month number (1-12).
The << operator works fine. Just need help with the >> operator.

.cpp
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
#include <iostream>
#include <string>
#include "Month.h"
using namespace std;

int main ()
{
	//int choice;
	//int monthnum;
	string Monthname;
	string Months[12] = { "January", "February", "March", "April", "May", "June", "July",
		"August", "September", "October", "November", "December" };

	int numOfMonth[12] = { 1,2,3,4,5,6,7,8,9,10,11,12 };

	Month testcase1;

	cout << "Please enter a number (1-12) to initialize the month object" << endl;
	cin >> testcase1;

	cout << testcase1;

	/*while (monthnum < 0 || monthnum > 12)
	{
		cout << "Invalid choice. Please enter a month (1-12) only!" << endl;
		cin.clear();
		cin.ignore();
		cin >> monthnum;
	}*/

	return 0;
}



*BELOW*
Look at line 127, am I doing this right?

.h
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
#ifndef Month_h
#define Month_h
#include <string>
#include <iostream>
using namespace std;

class Month
{
private:
	int monthNumber;
	string name;
public:

	string Months[12] = { "January", "February", "March", "April", "May", "June", "July",
	"August", "September", "October", "November", "December"};

	int numOfMonth[12] = {1,2,3,4,5,6,7,8,9,10,11,12};

	//default constructor
	Month()
	{
		monthNumber = 1;
		name = "January";
	}

	Month(string monthName)
	{
		for (int x = 0; x < 12; x++)
		{
			if (Months[x] == monthName)
			{
				name = Months[x];
				monthNumber = numOfMonth[x];
				break;
			}
		}
	}

	Month(int MonthNUM)
	{
		monthNumber = numOfMonth[MonthNUM - 1];
		name = Months[MonthNUM - 1];
	}

	void setName(string monthName)
	{
		for (int x = 0; x < 12; x++)
		{
			if (Months[x] == monthName)
			{
				name = Months[x];
				monthNumber = numOfMonth[x];
				break;
			}
		}
	}

	void setMonthNumber(int MonthNUM)
	{
		monthNumber = numOfMonth[MonthNUM - 1];
		name = Months[MonthNUM - 1];

		if (MonthNUM > 12)
		{
			monthNumber = numOfMonth[0];
			name = Months[0];
		}

		if (MonthNUM < 1)
		{
			monthNumber = numOfMonth[11];
			name = Months[11];
		}
	}

	int getMonthNum()
	{
		return monthNumber;
	}

	string getMonthName()
	{
		return name;
	}

	// overloaded ++ postfix operator function
		Month operator ++ (int) //dummy parameter, because of this, C++ will know this is postfix mode
	{
		Month temp;
		monthNumber++;
		setMonthNumber(monthNumber);
		return temp;
	}

	Month operator ++ () //prefix mode
	{
		Month temp;
		++monthNumber;
		setMonthNumber(monthNumber);
		return *this;
	}

	// overloaded ++ postfix operator function
	Month operator -- (int) //dummy parameter, because of this, C++ will know this is postfix mode
	{
		Month temp;
		monthNumber--;
		setMonthNumber(monthNumber);
		return temp;
	}

	Month operator -- () //prefix mode
	{
		Month temp;
		--monthNumber;
		setMonthNumber(monthNumber);
		return *this;
	}
	
	friend ostream &operator << (ostream &out, Month temp) //overloaded << (output) operator
	{
		out << "Month : " << temp.getMonthNum() << endl;
		out << "Month : " << temp.getMonthName() << endl;
		return out;
	}

	friend istream &operator >> (istream &in, Month temp) //overloaded >> (input) operator
	{
		int mnth;
		cout << "Enter Month Number: ";
		in >> mnth;
		return in;
	}

};
#endif // !Month_h
Last edited on
Look at line 127, am I doing this right?
No, you pass the object by value, i.e. a copy of the object. You should pass it by reference plus you should modify the passed object, instead you feed a local variable which is lost when the operator is done.
coder777,

Ok so how exactly can I return it?

I cannot figure it out for the life of me..
Here is what I have as of right now...

1
2
3
4
5
6
7
8
9

	friend istream &operator >> (istream &in, Month &temp) //overloaded >> (input) operator
	{
		int mnth;
		cout << "Enter Month Number: ";
		in >> mnth;
		return temp.setMonthNumber(mnth);
	}
closed account (48T7M4Gy)
return in;
kemort,

I did return in originally...

Similar to the code as I had in the original post. It didn't do what I intended. Once I used cout with the object in main, it defaulted to Setting the number as 1 and the month as January. Sad!

- MisterTams
closed account (48T7M4Gy)
Well, I haven't looked all that closely at the rest of your code. I concentrated on the return problem which solves that part AFAICS.

An observation on your months though. The index value of Months array (even the [12]) makes the numOfMonth array redundant.
string Months[] = { "dummy","January", "February", "March", "April", "May", "June", "July","August", "September", "October", "November", "December"}; is all that's required and everything follows from that.


closed account (48T7M4Gy)
Something to consider. Remove the redundant information being recorded by the data abstraction of a month being simple the month number.

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
using namespace std;

static const string Months[] =
{
    "dummy", "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
};

class Month
{
private:
    int monthNumber;

public:
    Month(){monthNumber = 1;}
    
    Month(string monthName){
        for (int x = 1; x <= 12; x++){ // oops!
            if (Months[x] == monthName){monthNumber = x;
            }
        }
    }
    
    Month(int MonthNUM){monthNumber = MonthNUM;}
    
    void setMonthNumber(string month_name){
        for (int x = 0; x < 12; x++){
            if (Months[x] == month_name){
                monthNumber = x;
            }
        }
    }
    
    int getMonthNum(){return monthNumber;}
    
    string getMonthName(){return Months[monthNumber];}
    
    friend ostream &operator << (ostream &out, Month temp){
        out
        << "Month : " << temp.getMonthNum() << ' '
        << "Month : " << temp.getMonthName() << endl;
        return out;
    }
    
    friend istream &operator >> (istream &in, Month &month){
        cout << "Enter month no: ";
        in >> month.monthNumber;
        return in;
    }
};

int main (){
    Month testcase1;
    cin >> testcase1;
    
    cout << testcase1.getMonthName() << '\n';
    cout << testcase1 << '\n';
    
    return 0;
}
Enter month no: 7
July
Month : 7 Month : July

Program ended with exit code: 0
Last edited on
Change the operator>> to:
1
2
3
4
5
6
7
	friend istream &operator >> (istream &in, Month temp) //overloaded >> (input) operator
	{
		int mnth;
		cout << "Enter Month Number: ";
		in >> monthNumbermnth;
		return in;
	}


Note that the operator<< does not match, hence writing to a file and reading from the same file will show the wrong results. A matching operator<< would be this:
1
2
3
4
5
6
7
	friend ostream &operator << (ostream &out, Month temp) //overloaded << (output) operator
	{
		out << "Month : " << temp.getMonthNum() << endl;
		out << "Month : " << temp.getMonthName() << endl;
		out << monthNumber << endl;
		return out;
	}


You can remove the member variable name as kemort hinted because you can obtain the names from the Months array.

Regarding the numOfMonth array: I would think you misunderstood the use of it. I would think that it contains the number of days of the month. I.e. int numOfMonth[12] = {31,28,31,...};
closed account (48T7M4Gy)
The possible misunderstanding of the numOfMonth array and 'repair' makes sense to me.

As a small embellishment two points:
1. [12] - the 12 again is redundant.
2. make the zero-th element -1 or 99 so that month[1] is directly/intuitively/conventionally mapped to "January", [12] to "December".
kemort and coder777,

You guys helped tremendously. It is a lot clearer to me now.
Thank you so very much!

Topic archived. No new replies allowed.