Hello guys,
I'm just getting in overloading, I'm trying to overload the stream, sum and product operators to correctly use them with a class named "Complex", that (as the name suggests) defines complex numbers. I have a strange problem with the '>>' operator though (this is the first time I'm dealing with overloading). Here is the code:
Insert a complex number (re,imm): (1,2)
Insert a complex number (re,imm): (2,3)
You have inserted c1(1,2) and c2(0,0)
c1 + c2: (1,2)
c1 * c2: (0,0)
The second object is always constructed by default, like I didn't input anything. Also, I noticed that if I construct a third object, the third cin isn't executed at all. I tried to write also "cin >> c1 >> c2;" but didn't change anything. I read everywhere, I can't understand where's the error, can you help me out please? Thanks a lot!
your >> does not remove a '\n'. this '\n' is found by any succeeding calls to >>. therefore the order of symbols is mixed (you try to read c.real, when '(' has not been ignored yet). This causes an error (flag) in istream object (which can be solved with cin.clear() ). You should change your last ingore() to ignore(2) (or for some safety ignore(numeric_limits<streamsize>::max(), '\n'); ).
You are a savior, thank you really much! So it was that extra space after the colon, messing up the ignore command, the cause of all?
EDIT: No, I'm sorry, I think I didn't understand this really well, the problem is the new line intended as the return key hit during input or the '\n' in the cout line? If so, does it mean that the stream is shared beetween input and output operations? I'm sorry if those are dumb questions, but I really want to understand well this...