char* frozenPlayers[32];
void AddFrozenPlayer(char* name){
for (int i = 0; i < 32; i++){
if (strcmp(frozenPlayers[i], name) == 0){ //already in the array, nothing to do
frozenPlayers[i] = name;
return;
}
}
}
void RemoveFrozenPlayer(char* name){
for (int i = 0; i < 32; i++){
if (strcmp(frozenPlayers[i], name) == 0){ //already in the array, nothing to do
frozenPlayers[i] = NULL;
return;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
AddFrozenPlayer("Player1");//Crashes on first call
AddFrozenPlayer("Player2");
RemoveFrozenPlayer("Player1");
for (int i = 0; i < 32; i++){
if (frozenPlayers[i] != NULL){
if (strcmp(frozenPlayers[i], "Player1") == 0)
cout << "Player1 is frozen" << endl;
elseif (strcmp(frozenPlayers[i], "Player2") == 0)
cout << "Player2 is frozen" << endl;
}
}
return 0;
}
I suggest you make a line at the beginning of your code that says something like: const size_t NUM_PLAYERS = 32; Then, everywhere you have a 32 in your code, replace it with NUM_PLAYERS. This way, you only have to change one line of code (the NUM_PLAYERS declaration) to change the number of players.
Your first line only declares that you have an array of pointers to chars. When you add a new name, you must allocate memory to stick the string of characters into.
First, initialize the array to get it into a known state (say, at the beginning of main):
1 2 3 4
for (int i = 0; i < NUM_PLAYERS; i++)
{
frozenPlayers[i] = nullptr;
}
In your AddFrozenPlayer code... describe your algorithm. What do you want to happen when you try to add a player? The code right now looks like it will write the new name into every slot in the array. I don't think this is what you want. If you describe more how your Add is supposed to work, I can help show you how to add a string properly.
#include <iostream>
#include <cstring>
using std::strcmp;
using std::strlen;
using std::cout;
using std::endl;
const size_t NUM_PLAYERS = 32;
char* frozenPlayers[NUM_PLAYERS];
//Gonna make this return bool so we can inform callers whether or not
//we actually added the name. True means we added the name, false means
//we didn't add it either because it was already there or we didn't have
//a spot in the array to put it.
bool AddFrozenPlayer(constchar* name)
{
//see if we already have this player
for (int i = 0; i < NUM_PLAYERS; i++)
{
if (frozenPlayers[i] != nullptr && strcmp(frozenPlayers[i], name) == 0)
{
//if this spot in the array has data in it
//AND that data matches the name we passed in
//already in the array, nothing to do
returnfalse;
}
}
//if we got this far then the name isn't in the array
//look for the first empty spot and put the new name there
for (int i = 0; i < NUM_PLAYERS; i++)
{
if (frozenPlayers[i] == nullptr) //if we found an empty spot in the array
{
frozenPlayers[i] = newchar[strlen(name + 1)]; //allocate enough memory to hold a copy
strcpy(frozenPlayers[i], name); //copy from the parameter into this newly allocated memory
returntrue;
}
}
//if we got this far, then we didn't add a name
returnfalse;
}
int main(int argc, char* argv[])
{
//initialize the frozenPlayers array with nullptrs so we start from a known
//state regarding the pointers in the array
for (int i = 0; i < NUM_PLAYERS; i++)
{
frozenPlayers[i] = nullptr;
}
//add Player1
if (AddFrozenPlayer("Player1"))
{
std::cout << "Successfully added Player1\n";
}
else
{
std::cout << "If you see this output, something went wrong!\n";
}
//try adding Player1 again
if (AddFrozenPlayer("Player1"))
{
std::cout << "Shouldn't see this! We can't add the same player twice...\n";
}
else
{
std::cout << "Couldn't add the same player twice, just as expected!\n";
}
}
Here's the remove logic. It is SO important that you clean up after yourself and reset the relevant pointer to null when removing a name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//Return value of true indicates a removal occured
//false indicates no removal (likely, name wasn't found)
bool RemoveFrozenPlayer(constchar* name)
{
for (int i = 0; i < NUM_PLAYERS; i++)
{
if (frozenPlayers[i] != nullptr && strcmp(frozenPlayers[i], name) == 0)
{
delete[] frozenPlayers[i];
frozenPlayers[i] = nullptr;
returntrue;
}
}
returnfalse;
}
And for good measure, since you initialized the array at the beginning of main, here's a way to clean up everything at the end of main:
1 2 3 4 5 6 7 8 9 10 11 12
//clean up the frozenPlayers array
//not a huge deal at the end of main since the OS will reclaim all memory
// when the process ends, but it is good discipline to
//clean up (i.e. delete and null out) pointers when you're done with them
for (int i = 0; i < NUM_PLAYERS; i++)
{
if (frozenPlayers[i] != nullptr)
{
delete[] frozenPlayers[i];
frozenPlayers[i] = nullptr;
}
}