943,808 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 664
  • C++ RSS
Mar 24th, 2009
0

help with overload of operator >>

Expand Post »
Hello, I have some weird problem with this code.
The problem is that when I use >> to read data a text file to vector. It will only add the first double and discard the rest. Anyway if I use array instead of vector it works fine. Anyone know what the problem can be? I've used cout to check whetever it sends in correct data or not. And it does send in correct data.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. struct Demo {
  7. string name;
  8. double points;
  9. vector<double> test;
  10. // double test[2];
  11. };
  12.  
  13. istream& operator>> (std::istream& in, Demo &d);
  14. istream& operator>> (std::istream& in, vector<double> d);
  15. //istream& operator>> (istream& in, double d[]);
  16. int main()
  17. {
  18. vector<Demo> d;
  19. Demo tmp;
  20. while(cin >> tmp) {
  21. d.push_back(tmp);
  22. }
  23. cout << d[0].name << ", " << d[0].points << ", " << d[0].test[0] << ", " << d[0].test[1] << endl;
  24. cout << d[1].name << ", " << d[1].points << ", " << d[1].test[0] << ", " << d[1].test[1] << endl;
  25. cout << d[2].name << ", " << d[2].points << ", " << d[2].test[0] << ", " << d[2].test[1] << endl;
  26. cout << d[3].name << ", " << d[3].points << ", " << d[3].test[0] << ", " << d[3].test[1] << endl;
  27. cout << d[4].name << ", " << d[4].points << ", " << d[4].test[0] << ", " << d[4].test[1] << endl;
  28. cout << d[5].name << ", " << d[5].points << ", " << d[5].test[0] << ", " << d[5].test[1] << endl;
  29. return 0;
  30. }
  31.  
  32. istream& operator>> (std::istream& in, Demo &d) {
  33. getline(in, d.name);
  34. in >> d.points;
  35. in >> d.test;
  36. return in;
  37. }
  38. std::istream& operator>> (std::istream& in, vector<double> d) {
  39. double x;
  40. d.clear();
  41. for(int i = 0; i < 2; i++) {
  42. in >> x;
  43. d.push_back(x);
  44. }
  45. in.ignore();
  46. return in;
  47. }
  48. //istream& operator>> (istream& in, double d[]) {
  49. // for(int i = 0; i < 2; i++) {
  50. // in >> d[i];
  51. // }
  52. // in.ignore();
  53. // return in;
  54. //}

the text file I use contains
C++ Syntax (Toggle Plain Text)
  1. Mario Pizza
  2. 2.3 159.9 1
  3. Luigi Kebab
  4. 2.6 169.5 2
  5. Princess Peach
  6. 2.6 169.5 3
  7. Link Hylia
  8. 2.6 169.5 4
  9. Zelda Hyrule
  10. 2.6 169.5 5
Reputation Points: 46
Solved Threads: 1
Light Poster
ganmo is offline Offline
38 posts
since Mar 2009
Mar 24th, 2009
0

Re: help with overload of operator >>

Pass the vector by reference just like you already pass the Demo object.
istream& operator>> (std::istream& in, vector<double> & d);
std::istream& operator>> (std::istream& in, vector<double> & d)

Maybe read about References
Last edited by mitrmkar; Mar 24th, 2009 at 7:46 am. Reason: Link
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Mar 24th, 2009
0

Re: help with overload of operator >>

hi,
i tried that before, but the problem with that is it will add all values into same vector<double> test. That's not want I wanted, I want to have the first double value in points, and the following two double values into unique vector<double> test for each user
Reputation Points: 46
Solved Threads: 1
Light Poster
ganmo is offline Offline
38 posts
since Mar 2009
Mar 24th, 2009
0

Re: help with overload of operator >>

Click to Expand / Collapse  Quote originally posted by ganmo ...
hi,
i tried that before, but the problem with that is it will add all values into same vector<double> test.
When you tried that and noticed that problem, are you sure that you did not have the d.clear(); call. Because the clear(), as you now have it, will empty the vector (of the tmp Demo object) so you'll not be accumulating the values in the vectors.

Maybe I'm not fully understanding you, because I'd still say that if you pass the vector by reference, you'll get what you want. I.e.

Quote ...
I want to have the first double value in points, and the following two double values into unique vector<double> test for each user
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Mar 24th, 2009
0

Re: help with overload of operator >>

Yes I removed clear(); when I tried it before. Anyway it works now when I put it back

Thanks alot for the help!
Reputation Points: 46
Solved Threads: 1
Light Poster
ganmo is offline Offline
38 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Making Derived Class Object Equal To Base Class Object
Next Thread in C++ Forum Timeline: .s3m player in c++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC