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
1 2 3 4 5 6 7 8 9 10 11
// 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;
}
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 :)
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 :)
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.