Strcmp -
Cowboy - 24.12.2011
Hello,
I am trying to make something like, when a gang gets created, it searches in one array, and find a gang name that matches with the one you have written as the gangname you want to make, and then send a message, sorry if it's not clear, maybe some code might help
pawn Код:
// Inside my gangcreate command, it prints incorrectly
for(new i = 1; i < GangInfo[i][gName]; i++)
{
printf("Gang Name: %s - Gang Name 2: %s\n", GangName, GangInfo[i][gName]);
if(strcmp(GangName, GangInfo[i][gName], true))
{
SendClientMessage(playerid, Red, "A gang with this name already exists");
return 1;
}
}
Thanks to funky1234 for helping a lot
Re: Strcmp -
Calgon - 24.12.2011
strcmp returns 0 if the condition is true, and 1 if it's not. Change your strcmp code line to this:
pawn Код:
if(!strcmp(GangName, GangInfo[i][gName], true))
Does that fix your problem?
The '!' (exclamation mark) checks if the value is 0 or if it's less than 0.
Re: Strcmp -
Cowboy - 24.12.2011
Hey, thanks for your answer!
It doesn't work, but it seems to be printing something new
Gang 1, prints what I have written as the new gangname, and gang 2 prints out all the gang names which exists like example
"Gang Name: Test - Gang Name 2: Gang"
"Gang Name: Test - Gang Name 2: Etc"
So it loops through all the existing gangs with the new gang name, but it just keeps allowing me to make a new gang, I hope you understand me, thanks again Calgon, appreciated.
Re: Strcmp -
Thresholdold - 24.12.2011
GangInfo[i][gName] change i to playerid?
Dunno if that would work :S
Re: Strcmp -
Cowboy - 24.12.2011
I am sure that won't work, but thanks anyways.
Re: Strcmp -
Ash. - 24.12.2011
Do you ever get the same string printed twice? By that I mean:
"Gang Name: Test - Gang Name 2: Test"
Re: Strcmp -
Ballu Miaa - 24.12.2011
I used for that kind of usage , strlen function . Tryna use that. I hope if helps and if it doesnt im sorry mate!
Re: Strcmp -
Cowboy - 24.12.2011
Silly me! It does work with existing names, but not the new created gangs. How would I be able to store the new gang name into that enum in gangcreate command?
Edit: Tried something like this but it does not work:
pawn Код:
GangInfo[GetGangID + 1][gName] = GangName;
Re: Strcmp -
Ash. - 24.12.2011
Quote:
Originally Posted by Cowboy
Silly me! It does work with existing names, but not the new created gangs. How would I be able to store the new gang name into that enum in gangcreate command?
Edit: Tried something like this but it does not work:
pawn Код:
GangInfo[GetGangID + 1][gName] = GangName;
|
You could do something like this:
pawn Код:
for(new i; i < sizeof(GangInfo); i++)
{
if(!strlen(GangInfo[i][gName])) //If it's empty (not used)
{
format(GangInfo[i][gName], MAX_GANG_NAME, TheVariableToStore); //Put the name in the variable that is empty.
break; //Exit the loop, it's no longer needed
}
}
It would be more efficient to create a variable and store in it the last used array slot, that way everytime a gang is created, you can just put the gane name in the slot contained in the variable, and then increment the variable containing the last array slot pointer.
Edit: Just taken a look at your loop in your original post. You should use 'sizeof(GangInfo)' (or a size definition) rather than the actual GangInfo[i][gName] identifier in your loop header. (Take a look at my example above)
Re: Strcmp -
Cowboy - 24.12.2011
Where do I put your piece of code exactly?
Also I can do that with sizeof(GangInfo), what's the difference though?