Strange behaviour of overloaded '>>' operator.

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:

Header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iomanip>

using namespace std;

#ifndef COMPLEX_H
#define COMPLEX_H

class Complex {
friend ostream& operator<<(ostream&, const Complex&);
friend istream& operator>>(istream&, Complex&); 
friend Complex operator*(const Complex&, const Complex&); 
friend Complex operator+(const Complex&, const Complex&); 

public:
Complex(int = 0, int = 0); // Default constructor
Complex(const Complex&); // Copy constructior

private:
	int real;
	int imm;
};

#endif 


Functions:
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
#include "complex.h"

Complex::Complex(int r, int i){
	real = r;
	imm = i;
}

Complex::Complex(const Complex &c){
	real = c.real;
	imm = c.imm;
}

ostream& operator<<(ostream &output, const Complex &c){
	output << "(" << c.real << "," << c.imm << ")";
	return output;
}
istream& operator>>(istream &input, Complex &c){
	input.ignore(); // Ignore "("
	input >> setw(2) >> c.real; // Read real part
	input.ignore(); // Ignore ","
	input >> setw(2) >> c.imm; // Read immaginary part
	input.ignore(); // Ignore ")"
	return input;
}

Complex operator*(const Complex &c1, const Complex &c2){
	Complex cprod;
	cprod.real = ((c1.real * c2.real) - (c1.imm * c2.imm));
	cprod.imm = ((c1.imm * c2.real) + (c1.real * c2.imm));
	return cprod;
}

Complex operator+(const Complex &c1, const Complex &c2){
	Complex csum;
	csum.real = c1.real + c2.real;
	csum.imm = c1.imm + c2.imm;
	return csum;
}


Main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "complex.h"

int main(){
	Complex c1, c2;

	cout << "Insert a complex number (re,imm): ";
	cin >> c1;
	cout << "\nInsert a complex number (re,imm): ";
	cin >> c2;
	cout << "\nYou have inserted c1" << c1 << " and c2" << c2 << endl;
	cout << "c1 + c2: " << c1 + c2 << endl;
	cout << "c1 * c2: " << c1 * c2 << endl;

	return 0;
}


This is the output I get:

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!
Last edited on
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...
Last edited on
It's the enter key. It was still unread, so you should clear the buffer.
Topic archived. No new replies allowed.