help with overload of operator >>

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 37
Reputation: ganmo is on a distinguished road 
Solved Threads: 0
ganmo ganmo is offline Offline
Light Poster

help with overload of operator >>

 
0
  #1
Mar 24th, 2009
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.
  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
  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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 979
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 209
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: help with overload of operator >>

 
0
  #2
Mar 24th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 37
Reputation: ganmo is on a distinguished road 
Solved Threads: 0
ganmo ganmo is offline Offline
Light Poster

Re: help with overload of operator >>

 
0
  #3
Mar 24th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 979
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 209
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: help with overload of operator >>

 
0
  #4
Mar 24th, 2009
Originally Posted by ganmo View Post
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.

I want to have the first double value in points, and the following two double values into unique vector<double> test for each user
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 37
Reputation: ganmo is on a distinguished road 
Solved Threads: 0
ganmo ganmo is offline Offline
Light Poster

Re: help with overload of operator >>

 
0
  #5
Mar 24th, 2009
Yes I removed clear(); when I tried it before. Anyway it works now when I put it back

Thanks alot for the help!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC