Not Initializing

The code is compiling but after input secondVector, it doesn't initialize it with the given value. Here's the main function:

int main()
{
Vector3D firstVector = Vector3D();
Vector3D secondVector = Vector3D();
cout << "Enter two vectors in format (x, y, z): " << endl << "First Vector is ";
cin >> firstVector;
cout << "Second vector is ";
cin >> secondVector;

Vector3D sumVector(firstVector + secondVector);
cout << endl << sumVector << endl;

return 0;
}

And this is the istream inplementation:

std::istream& operator>>(std::istream &iStream, Vector3D &vector)
{
iStream.ignore();
iStream >> vector.m_X;
iStream.ignore(2);
iStream >> vector.m_Y;
iStream.ignore(2);
iStream >> vector.m_Z;
return iStream;
}

I'm confused because it initializes the firstVector properly. Could somebody help?
Last edited on
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
int main()
{
   Vector3D firstVector = Vector3D();
   Vector3D secondVector = Vector3D();
   cout << "Enter two vectors in format (x, y, z): " << endl << "First Vector is ";
   cin >> firstVector;
   cout << "Second vector is ";
   cin >> secondVector;

   Vector3D sumVector(firstVector + secondVector);
   cout << endl << sumVector << endl;

   return 0;
}

And this is the istream inplementation:

std::istream& operator>>(std::istream &iStream, Vector3D &vector)
{
   iStream.ignore();
   iStream >> vector.m_X;
   iStream.ignore(2);
   iStream >> vector.m_Y;
   iStream.ignore(2);
   iStream >> vector.m_Z;
   return iStream;
}


I theorize, based on your prompts, that the input would be something like, say, (5, 2, 5) and your overload for the stream extraction operator:
(1) Ignores "("
(2) Puts 5 into X
(3) Ignores ", "
(4) Puts 2 into Y
(5) Ignores ", "
(6) Puts 5 into Z
(7) Returns the istream object.

However, you haven't discarded ")\n" from the stream after you read in vector.m_Z, so that remains in the input buffer until the next read, which is throwing off the input of your second vector.

After you assign to vector.m_Z, consider ignoring whatever else is in the input stream with:

iStream.ignore(numeric_limits<streamsize>::max());

to throw out whatever is left in the input buffer after you've collected your last vector component.
Last edited on
Sorry for the stupid question, but what I should add except this line, some library or definition?
Last edited on
I think you may have to #include the <limits> library. See if that works for you :)
I added <limits> but it still underlines numeric_limits and streamsize as undefined and about max it says that tha global scope has no max.
Why don't we try something a bit more simple, hmm? :)

Lets just delete the <Limits> include and switch out the:

iStream.ignore(numeric_limits<streamsize>::max());

with something like:

iStream.ignore(1000);

That way, if the program works, we've narrowed the issue down to the input stream retaining additional characters in the input buffer and can refine the last ignore statement to be more precise with all of the bells and whistles. Let me know how that goes.
With iStream.ignore(1000); is not better because it wants to be typed a thousand symbols before countinue running, but I tried with iStream.ignore(2) and it works properly. Thank you very much for your help :)
Well, a bit of a round-about solution, but I'm glad I could help! :)
Topic archived. No new replies allowed.