Small problem about foreach and strcmp
#1

Hi, i have a small problem with this function :

PHP код:
stock CheckServerIp(string[]) {
    foreach(new 
iVirtualServer) if(!strcmp(VirtualServerInfo[i][sIP], string)) return i;
    return -
1;

The problem is that this function always return the first value, for example i have the valid VirtualServer value 1,2,3 and the string that i want to get is 3 but it will return 1, i have done some test commands and it's work perfect, this is my test commands below:
PHP код:
CMD:getip(playeridparams[]) {
    new 
value;
    if(
sscanf(params,"d"value)) return Usage(playerid"/getip [id]");
    return 
SendFormatMessage(playerid, -1"%s"VirtualServerInfo[value][sIP]);

Thanks for reading.
Reply
#2

strcmp returns 0 if either of the strings are empty. Are you sure the string actually contains something?
Reply
#3

Yes i'am sure, my test command work and always return the correct value, and i would like to say that this function don't return 0, it's return the first value of the loop, because i have try to edit my function to

PHP код:
stock CheckServerIp(string[]) { 
    foreach(new 
iVirtualServer) if(!strcmp(VirtualServerInfo[i][sIP], string) && != 0) return i;  // return 1
    
return -1

stock CheckServerIp(string[]) { 
    foreach(new 
iVirtualServer) if(!strcmp(VirtualServerInfo[i][sIP], string) && != && != 1) return i;  // return 2
    
return -1

Reply
#4

Okay, what are the contents of "VirtualServerInfo[0][sIP]", "VirtualServerInfo[1][sIP]" and "VirtualServerInfo[2][sIP]" when you run those functions? (An example, not actual IPs)
Reply
#5

I have try this

PHP код:
CMD:getip0(playeridparams[]) return SendFormatMessage(playerid, -1"%s"VirtualServerInfo[0][sIP]); // correct
CMD:getip1(playeridparams[]) return SendFormatMessage(playerid, -1"%s"VirtualServerInfo[1][sIP]); // correct
CMD:getip2(playeridparams[]) return SendFormatMessage(playerid, -1"%s"VirtualServerInfo[2][sIP]); // correct 
and i have a system to see the IP in dialog and it's correct (working fine), i think this can be provoked by the loop.
Reply
#6

You can reverse the iterator and it's always good to check if the string is not null before comparing it with another string.

PHP код:
CheckServerIp(string[])
{
    foreach(new 
Reverse(VirtualServer))
    {
        if (!
isnull(VirtualServerInfo[i][sIP]) && !strcmp(VirtualServerInfo[i][sIP], string)) return i;
    }
    return -
1;

Reply
#7

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
You can reverse the iterator and it's always good to check if the string is not null before comparing it with another string.

PHP код:
CheckServerIp(string[])
{
    foreach(new 
Reverse(VirtualServer))
    {
        if (!
isnull(VirtualServerInfo[i][sIP]) && !strcmp(VirtualServerInfo[i][sIP], string)) return i;
    }
    return -
1;

Looks like the same. (same problem)
Reply
#8

Bump
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)