3

I have this bit of code:

... 
ComplexNumber C1;
ComplexNumber C2;

cout << "Enter a complex number C1:" << endl;
cin >> C1;
cout << C1 << endl;
cout << "Enter a complex number C2:" << endl;
cin >> C2;
cout << C2 << endl;
...

but as I've discovered it won't wait for user input the second time and will simply leave C2 with the default value I've defined for the zero-arg constructor in the ComplexNumber class and move on.

All the solutions I've found to this issue use getline() instead of cin >> , but this assignment is to test how well we've overloaded the opperator >> for our ComplexNumber class, so I think using getline would defeat that purpose. Is there another way to make this work?

EDIT: @Martin you were correct! It works after I changed my operator>> to:

istream & operator>>(istream & in, ComplexNumber & n) 
{
    int inreal=0;
    int inimag=0;
    in >> inreal;
    char plus;
    in.get(plus); // read the plus sign since it's a char
    in >> inimag;
    char i;  // DID NOT HAVE THIS LINE AT FIRST
    in.get(i); // DID NOT HAVE THIS LINE AT FIRST
    n = ComplexNumber(inreal,inimag);
    return in;
}

Thank you so much!

Since I'm new to the forum I don't know how to give credit to a sub-comment; is it okay if I just give the green check to the one official reply on this post?

Martin York
  • 257,169
  • 86
  • 333
  • 562
Liz Z
  • 85
  • 1
  • 8
  • possible duplicate of [Problem of using cin twice.](http://stackoverflow.com/questions/2525352/problem-of-using-cin-twice) – wallyk Apr 20 '11 at 23:31
  • 2
    Exactly what did you type on the keyboard (including the return and any control keys you pushed). Also how did you define the operator >> for COmplexNumber – Martin York Apr 20 '11 at 23:33
  • Do you mean while testing? I entered: 5+9i[enter] It read back C1 as expected from my print method, then ran through the rest of the output only. – Liz Z Apr 20 '11 at 23:43
  • That looks good. But I bet you did not read the `i` character from the input stream! But until you show the code for your operator >> it is **IMPOSABLE** to give you a real answer. – Martin York Apr 20 '11 at 23:51

2 Answers2

2

You need to clear cin. It's reading in the 'enter' from the previous cin.

cin.clear();

And I also remember doing something along the lines of:

cin.ignore(cin.rdbuf()->in_avail());

More information in this post:

How do I flush the cin buffer?

Community
  • 1
  • 1
Tyler Ferraro
  • 3,753
  • 1
  • 21
  • 28
  • Thank you; I did read that post, but I'm not sure where to put this in my code. I tried after the cout << C1 line, but that didn't change anything. – Liz Z Apr 20 '11 at 23:37
  • You should do it right after reading the first cin, or right before reading the second cin. It's clearing the buffer so that cin doesn't read in the input (enter) from the previous input, thus allowing you to give a second input. – Tyler Ferraro Apr 20 '11 at 23:40
  • After reading the first cin I added first one, then both of the lines Tyler suggested (and previously tried other suggestions from the 'flush the cin buffer' question) and recompiled, but the code still behaves the same. I am baffled. – Liz Z Apr 20 '11 at 23:53
1

I bet you did not read the i character from the input stream

Martin York
  • 257,169
  • 86
  • 333
  • 562