pointers

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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;
  
}


Is displayCarts(vector<SnackCart*> cart, int count) expected to print all the elements in cart? It doesn't.

What does parameter count do? A vector knows its own size.
sorry this is how the function really is.
the << has been overloaded to display details of the respective cart objects.

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

But it doesn't need to be.

1
2
3
4
5
6
7
8
9
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).
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 :)
Yes, sorry, my cout should have been:

 
std::cout << **c << std::endl;

why is there a need to dereference it twice? (pardon my excessive questioning. I'm a first year IT student who is just getting into programming :) )
Last edited on
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.
so iterators are essentially pointers?
Yes.
thanks :)
Topic archived. No new replies allowed.