While reading from the file, the function reads everything but prints only the first snackcart and the third snackcart twice. I have no idea why. Code: int loadInfoCart(string filename, vector<SnackCart*> &cart, int &cartCount,SnackCart *carts) { cart.push_back(new SnackCart); ifstream dataFile(filename.c_str()); cartCount = 0; string type, owner; while (!dataFile.eof()){ getline(dataFile, type); if (type == "Snackcart"){ cart.push_back(carts); getline(dataFile,owner); cart[cartCount]->setOwner(owner); string location; getline(dataFile,location); cart[cartCount]->setLocation(location); cart[cartCount]->clearMeals(); bool moreMeals = true; while (moreMeals) { string meal; getline(dataFile,meal); if (meal == "#") { moreMeals = false; } else { string numStr; getline(dataFile,numStr); float num = (float) atof(numStr.c_str()); string ratingStr; getline(dataFile,ratingStr); cart[cartCount]->loadaddMeal(meal,num,ratingStr); } } ++cartCount; } if (dataFile.eof()) break; } dataFile.close(); return cartCount; } text file Snackcart nik city chips 2 Average coke 1.8 Good # Snackcart nikhil cairns roll 2 Good sandwich 6 Bad # Snackcart juan brisbane muffin 2 Bad crisps 3 Average # Restaurant Mark 30, Sheridan Street 40221141 9am-10pm Pizza 15 Good Steak 20 Average # Restaurant Steve 12,Lake Street 12345678 11am-11pm rice 4 Good noodles 23 Bad # function to display data Code: // tell cart objects to display themselves void displayCarts(vector<SnackCart*> cart, int count) { if (count == 0) { cout << "Error: no carts added or loaded yet" << endl; return; } cout << *cart[1]; cout << endl; }
Code: void displayCards( const vector<SnackCart*>& carts ) { if( carts.empty() ) { std::cout << "Error: no cards added or loaded yet" << std::endl; return; } for( vector<SnackCart*>::const_iterator c = carts.begin(), c != carts.end(); ++c ) std::cout << *c << std::endl; } (It could be simplified even further with boost::lambda, but I won't even go there).
sorry this is how the function really is. the << has been overloaded to display details of the respective cart objects. sorry this is how the function really is. the << has been overloaded to display details of the respective cart objects. Code: void displayCarts(vector<SnackCart*> cart, int count) { if (count == 0) { cout << "Error: no carts added or loaded yet" << endl; return; } for (int i = 0; i < count; ++i){ cout << *cart[i]; cout << endl; } }
dereferencing of c is still displaying the address and not the contents at the address and from that point on. also 3 of the addresses are still the same. so I'm guessing it hasn't worked. I have read through my code loads of times. but i still cant find the source of the error. appreciate the help though
c is an iterator. To "get at" the element referenced by c, you just dereference c. That is the first dereference. The element is actually a pointer to SnackCart (ie, vector<SnackCart*>). We have to dereference that pointer also to get at the actual SnackCart instance.
i figured out why it wasn't working. It was because on line 7 i wasn't creating new SnackCart type objects to the vector and hence it only displayed every snackcart it came across after a certain gap. Thanks again for all the help