Reading file into structure (c++)

c++, data-structures, vector

Solution

The error arises from this line in the `reading_wishlist` function

`wish_list.budget<<money;`

You cannot use `<<` operator with a double as the left hand side (here WishList::budget). Do you mean

`wish_list.budget = money;`

Problem

I am very very new to C++, and I would like to read a text file into a structure. The text file has a double on the first line, and the lines after that exist as gift names (wishes). I created a struct, Wishlist, that exist as a double and a vector of wishes. So I did the following: ``` #include <iostream> #include <fstream> #include <vector> #include <string> using namespace std; struct Gift { double price; string name; }; typedef vector<Gift> Giftstore; typedef vector<string> Wishes; int size(Giftstore& g) {return static_cast<int>(g.size());} int size(Wishes& w) {return static_cast<int>(w.size());} struct Wishlist { double budget; Wishes wishes; }; void reading_wishlist(ifstream& file, Wishlist& wish_list) { if (file) { double money; file>>money; wish_list.budget<<money; } while(file) { string name; getline(file, name) wish_list.wishes.push_back(name); } file.close(); }; void print(Wishlist wish_list) { cout<<"Budget: "<<wish_list.budget<<endl; cout<<"Wishes: "<<endl; for(int i=0; i<size(wish_list.wishes()); i++) { cout<<wish_list.wishes[i]<<endl; } }; int main () { ifstream file; string filename; cout<<"Give a wishlist file: "; cin>>filename; file.open(filename) reading_wishlist(filename, wish_list); print(wish_list) return 0; } ``` Of course, while trying to build and run this, I again won some error prizes! The first one, is saying: (in reference to: wish_list.budget< Invalid operands of types 'double' and 'double' to binary operator<< What does this mean? Do I have to redefine the operator << ? Or can I read the double as a Cstring and then change it to double? What is the best way to deal with this? Better: how to read different types from a file? Since I also have to read a file into a structure, Giftstore, where the text file will consist of a double and a gift name on each line.

Original source