Vectors and pointers

The following code crashes when it reaches the diner[resCount]->setOwner
Any idea why?

diner is declared as vector<Restaurant*> diner

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
40
41
42
43
44
45
46
47
48
 int loadInfoRes(string filename, vector<Restaurant*> &diner,int &resCount) 
{
  diner.resize(100);
  ifstream dataFile(filename.c_str());
  resCount = 0;
  string type, owner;
  while (!dataFile.eof()){
	  getline(dataFile, type);
  
	  if (type == "Restaurant"){
	  		  getline(dataFile,owner);
			  diner[resCount]->setOwner(owner);				  
			  string address;
			  getline(dataFile,address);
			  diner[resCount]->setAddress(address);			  
			  string phno;
			  getline(dataFile, phno);
			  diner[resCount]->setPhoneNo(phno);
			  string ophrs;
			  getline(dataFile, ophrs);
			  diner[resCount]->setOpHrs(ophrs);
			  diner[resCount]->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);				  
								diner[resCount]->loadaddmeal(meal,num,ratingStr);
								}
							}
	  		
				++resCount;
  }
	  if (dataFile.eof())
		  break;
  }
  dataFile.close();
  return resCount;
}
  
 

Last edited on
Could you fix your indentation please? Then I'll look at it...
closed account (S6k9GNh0)
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
40
41
42
43
44
45
46
47
48
49
50
int loadInfoRes(string filename, vector<Restaurant*> &diner,int &resCount) 
{
	diner.resize(100);
	ifstream dataFile(filename.c_str());
	resCount = 0;
	string type, owner;
	while (!dataFile.eof())
	{
		getline(dataFile, type);
  
		if (type == "Restaurant")
		{
			getline(dataFile,owner);
			diner[resCount]->setOwner(owner);				  
			string address;
			getline(dataFile,address);
			diner[resCount]->setAddress(address);			  
			string phno;
			getline(dataFile, phno);
			diner[resCount]->setPhoneNo(phno);
			string ophrs;
			getline(dataFile, ophrs);
			diner[resCount]->setOpHrs(ophrs);
			diner[resCount]->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);				  
					diner[resCount]->loadaddmeal(meal,num,ratingStr);
				}
			}
	  		
			++resCount;
		}
		if (dataFile.eof())
			break;
	}
	dataFile.close();
	return resCount;
}
Thanks.

On line 3 you resize() diner, but the pointers inside it are still pointing at random garbage. Then you attempt to access the data stored there, which could be anything. You will need to set them to something before you use them (probably using new, just don't forget to delete them when you are done).
But since I have an arbitrary number of items in the text file how do I create an array of an unknown size? and thanks to the person above for setting my indentation right :)
also when i use the displayRestaurant function to display the contents it displays the address of where the vector is at and not the contents themselves. dereferencing it is not working for some reason.

1
2
3
4
5
6
7
8
9
10
void displayRestaurants(vector<Restaurant*> diner, int count) {
  if (count == 0) {
    cout << "Error: no restaurants added or loaded yet" << endl;
    return;
  }
  for (int i = 0; i < count; ++i) {
    cout << diner[i];
    cout << endl;
  }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
ostream& operator<<(ostream& os, Restaurant &dine){

	os << "Owner: " << dine.getOwner() << endl
		<< "Address: " << dine.getAddress() << endl
		<< "Phone Number: " << dine.getPhoneNo() << endl
		<< "Operating hours: " << dine.getOpHrs() << endl
		<< "Number of meals offered: " << dine.mealCount() << endl;
	for (int i = 0; i < dine.count; ++i){
		os << "Name: " << dine.meal[i].getName() << endl;
		os << "Cost: $" << dine.meal[i].getCost() << endl;
		os << "Rated: " << dine.meal[i].getRatingStr() << endl; 
	}
	return os;
}



Since you have a vector, just use the .push_back() (you don't have to resize it or anything, it does it automatically).

And the reason it is displaying the address is because you are printing out diner[i], which is a pointer. You want to dereference it first.
Could you please give me an example of how to use push_back in a similar case?
1
2
3
4
5
6
7
8
9
10
std::vector <obj*> objects;
objects.push_back(new obj); //push_back a new object
//...
for(unsigned int i = 0; i < objects.size(); ++i) {
    objects[i]->foo(); //now we have an obj* in objects that points to something
}
//...
for(unsigned int i = 0; i < objects.size(); ++i) {
    delete objects[i]; //make sure to delete them, since you newed them
}
Got it. Thanks heaps mate :)
Topic archived. No new replies allowed.