strcmp phone number - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: strcmp phone number (
/showthread.php?tid=533319)
strcmp phone number -
n00el - 23.08.2014
Whats wong with this?
Код:
if(strcmp(inputtext, "911") == 0)
{
//do something if inputtext 911 (police, ems etc.)
}
else
{
foreach(Player, i)
{
if(strcmp(PlayerInfo[i][pPhnumber], inputtext) == 0)
{
// do smoething
}
else
{
// number is not available
}
}
}
Re: strcmp phone number -
MissionCoder - 23.08.2014
The problem with this code is that your else statement is going to be called every iteration(every time it is repeated) in foreach when the number does not match.
What is usually done when such loops are implemented are "return values" Usually - what happens is that when a value is returned by a function, all of the code that is later in the function is disregarded and the function stops running - allowing to move on with the next operation.
Therefore your code would look like this:
pawn Код:
if(strcmp(inputtext, "911") == 0)
{
//do something if inputtext 911 (police, ems etc.)
return 1;
}
else
{
foreach(Player, i)
{
if(strcmp(PlayerInfo[i][pPhnumber], inputtext) == 0)
{
// do smoething
return 1;
}
}
// number not available
return 0;
}
P.S. I am not too sure at what level for scripting you are - but if you didn't get it - I'll happily reword it for you if you could tell me the exact part you did not understand
Re: strcmp phone number -
HazardouS - 23.08.2014
EDIT: My bad, i forgot that the ignorecase is optional.
Re: strcmp phone number -
n00el - 24.08.2014
Thanks! But, if is not 911, then the // number not available is always called, not?
For e.g. 123 is a vaild phone number, do something, and do the // number not available?