vector<structure> object(array) question

I have a vector that contains structures, the vector has a total of 4 elements in it of this structure, the structure has 4 fields, here is the structure:

1
2
3
4
5
6
7
struct PlayerSlot
{
	int id;
	string guid;
	string address;
	SystemAddress systemaddress;
};


Now in the code, I am creating the vector as follows:
 
vector<PlayerSlot> PlayerSlots(numPlayers);


Then I slate each one as an empty player with index as follows:
1
2
3
4
5
6
7
8
// 4 player connections to monitor
for(int i = 0 ; i < numPlayers; i++)
{
	PlayerSlots[i].id=i;
	PlayerSlots[i].address="";
	PlayerSlots[i].guid="";
	PlayerSlots[i].systemaddress = UNASSIGNED_SYSTEM_ADDRESS;
}


All is well so far, now I fill the player slot as follows: (based on if I find them in the list or not)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// look to see if the player is already in the list, if not add them
for(int i=0;i<numPlayers;i++)
{
 if(PlayerSlots[i].guid==p->guid.ToString())
  {
    slotInfo = &PlayerSlots[i];
    printf("Found: %s At Position: %d\n",slotInfo->guid,i);
    found=true;
  }
}
// find an empty slot
if(!found)
{
  for(int x=0;x<numPlayers;x++)
  {
     if(PlayerSlots[x].guid=="")
     {
	PlayerSlots[x].address=p->systemAddress.ToString(true);
	PlayerSlots[x].guid=p->guid.ToString();
	PlayerSlots[x].systemaddress=p->systemAddress;
	x=numPlayers-1;
    }
  }
}


Now the problem, the value from the "PRINTF" statement is NULL for the string and a huge negativfe number for the index, here is a video of the issue.

http://screencast.com/t/OWEyYmM0M

I put this in thew newbie section because I am new to C++ and I have to be doing something totally unknown for this not to print my information correctly. I am at a loss.

Thanks for any help you can possibly give.
Best regards
Last edited on
There are a few things wrong with your code. I will focus on the two that I think are most important.

First, it would be much easier and safer to just create a default constructor for your PlayerSlot struct than rely on external initialization.

1
2
3
4
5
6
7
8
9
10
11
struct PlayerSlot
{
	int id;
	string guid;
	string address;
	SystemAddress systemaddress;

        PlayerSlot()
        : id(0), guid(), address(), systemaddress(UNASSIGNED_SYSTEM_ADDRESS)
        {}
};


Second, rather than fully populate the vector with empty players, you probably should be constructing an empty vector and pushing constructed players into it. That way you don't have to worry about finding an empty slot.
Last edited on
Thanks a lot for that advice, I have made the code changes and am going to use push_back to add items into the vector list, I am still stuck at the moment as to why when I find a player in the vector, that I can not use "printf" to print the information that I am trying to show at the console. The guid still comes back as null and the id shows some wierd huge number like I show in my video that I have linked in the original question.

I like your idea for the self initialization and being new at C++, I wasn't sure how to self initialize the structure so this is very helpfull information.

Do you have any advice as to why printf would return null for the string when the guid clearly has a value and can be seen in debug? Is there something I am missing?
Thank you very much again for your help, it is truely appreciated.

Here is what I now have, I switched up the vector instansiate as follows given your instruction:
1
2
3
4
5
6
7
8
9
10
11
12
struct PlayerSlot
{
	size_t id;
	string guid;
	string address;
	SystemAddress systemaddress;

	PlayerSlot()
        : id(0), guid(), address(), systemaddress(UNASSIGNED_SYSTEM_ADDRESS)
        {}
};
vector<PlayerSlot> PlayerSlots;


Now I have the PlayerSlots holding nothing until something is pushed onto the stack, here is that code:

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
// look to see if the player is already in the list, if not add them
for(size_t i=0;i<PlayerSlots.size();i++)
{
	if(PlayerSlots[i].guid==p->guid.ToString() && !found)
	{
		slotInfo = &PlayerSlots[i];
		tmpstr = slotInfo->guid;
		// CORRECTLY SHOWS INFO
		std::cout << "Found Player GUID: " << slotInfo->guid << std::endl << "Position: " << i << std::endl << std::endl;

		// INCORRECTLY SHOWS INFO, tmpstr does have the guid but printf shows null
		// i has a value of 0 but shows 127
		printf("Found: %s At Position: %d \n",tmpstr,i);
		found=true;
		i=PlayerSlots.size()-1;
	}
}
// push the player onto the stack at the end
if(!found)
{
	newPlayer.address=p->systemAddress.ToString(true);
	newPlayer.guid=p->guid.ToString();
	newPlayer.id=PlayerSlots.size()+1;
	newPlayer.systemaddress=p->systemAddress;
	PlayerSlots.push_back(newPlayer);
}


As noted in the code, printf is still not displaying correct information, just junk. If I use cout, I get the right values on the console, still boggles my mind as to why.
Last edited on
One typically shuns printf() in C++ because it is not type safe. You are running into this issue head-first. One uses iostreams instead.

You could use printf() when forced to, but in this cast, you need to do this:

printf("Found: %s At Position: %d\n",slotInfo->guid.c_str(),i);

printf's %s doesn't work with C++ strings.
Thanks for the advice, that works! Not only codes the addition of the c_str() fix the %s but it also fixed the %d, I assume this is due to memory overflow or something strange, I will mark this solved now thanks to your major help PanGalactic! Now back to coding the player markers which is a lot more complicated.
Topic archived. No new replies allowed.