| | |
help with overload of operator >>
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2009
Posts: 37
Reputation:
Solved Threads: 0
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.
the text file I use contains
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)
#include <iostream> #include <string> #include <vector> using namespace std; struct Demo { string name; double points; vector<double> test; // double test[2]; }; istream& operator>> (std::istream& in, Demo &d); istream& operator>> (std::istream& in, vector<double> d); //istream& operator>> (istream& in, double d[]); int main() { vector<Demo> d; Demo tmp; while(cin >> tmp) { d.push_back(tmp); } cout << d[0].name << ", " << d[0].points << ", " << d[0].test[0] << ", " << d[0].test[1] << endl; cout << d[1].name << ", " << d[1].points << ", " << d[1].test[0] << ", " << d[1].test[1] << endl; cout << d[2].name << ", " << d[2].points << ", " << d[2].test[0] << ", " << d[2].test[1] << endl; cout << d[3].name << ", " << d[3].points << ", " << d[3].test[0] << ", " << d[3].test[1] << endl; cout << d[4].name << ", " << d[4].points << ", " << d[4].test[0] << ", " << d[4].test[1] << endl; cout << d[5].name << ", " << d[5].points << ", " << d[5].test[0] << ", " << d[5].test[1] << endl; return 0; } istream& operator>> (std::istream& in, Demo &d) { getline(in, d.name); in >> d.points; in >> d.test; return in; } std::istream& operator>> (std::istream& in, vector<double> d) { double x; d.clear(); for(int i = 0; i < 2; i++) { in >> x; d.push_back(x); } in.ignore(); return in; } //istream& operator>> (istream& in, double d[]) { // for(int i = 0; i < 2; i++) { // in >> d[i]; // } // in.ignore(); // return in; //}
the text file I use contains
C++ Syntax (Toggle Plain Text)
Mario Pizza 2.3 159.9 1 Luigi Kebab 2.6 169.5 2 Princess Peach 2.6 169.5 3 Link Hylia 2.6 169.5 4 Zelda Hyrule 2.6 169.5 5
•
•
Join Date: Nov 2007
Posts: 979
Reputation:
Solved Threads: 209
Pass the vector by reference just like you already pass the Demo object.
Maybe read about References
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
•
•
Join Date: Nov 2007
Posts: 979
Reputation:
Solved Threads: 209
•
•
•
•
hi,
i tried that before, but the problem with that is it will add all values into same vector<double> test.
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
![]() |
Similar Threads
- Overload operator with one operand. (C++)
- Overload Operator<< for a Binary Search Tree. Getting C.E. C3861 (C++)
- Problems with overload operator method (C++)
- help (overload << in a template class ) (C++)
- How to overload + operator for STL Vectors (C)
- How do I overload [] operator for pointers (C++)
- C++ Tic Tac Toe using classes & operator overloading (C++)
- Trying to overload + for class to add to string 'is illegal' (C)
- Help with operator overloading of << (C)
- need help with simple overloading operator+ for adding two object data. (C++)
Other Threads in the C++ Forum
- Previous Thread: Making Derived Class Object Equal To Base Class Object
- Next Thread: .s3m player in c++
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






