Stock Warning - 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: Stock Warning (
/showthread.php?tid=601217)
Stock Warning -
Ilhan - 18.02.2016
Hi, i wrote a function for making a calculation but it keeps giving warnings. I tried to fix warning but in that case it gives another warning. Those warnings doesn't affecting something but it's the only warning at my script and this is annoying me.
Code Option #1
Warning: warning 225: unreachable code (pointing "return -1;")
Код:
stock stockname(some params)
{
//my code here
for(;;i++)
{
//more code
if(...)
return i;
}
return -1;
}
Code Option #2
Warning: function "stockname" should return a value
Код:
stock stockname(some params)
{
//my code here
for(;;i++)
{
//more code
if(...)
return i;
}
}
How can i get rid of that warning?
Re: Stock Warning -
zPain - 18.02.2016
Both warnings are pretty self-explanatory. Do you mind showing what's inside the for loop?
Re: Stock Warning -
Ilhan - 18.02.2016
Quote:
Originally Posted by zPain
Both warnings are pretty self-explanatory. Do you mind showing what's inside the for loop?
|
Warnings are pretty clear but i still can't get rid of them. Or should i have used a do-while loop
Код:
stock stockname(param)
{
new i=1, mp=4, pp=2, total=0;
for(;;i++)
{
total += (i * mp) + pp;
if(total >= param)
return i;
}
//return -1;
}
Re: Stock Warning -
Gammix - 18.02.2016
First one appears because its a never ending loop so you'll never get to
return -1.
The second one is what compiler expects to get but it doesn't know ots an infinite loop.
To avoid, you'll need a limit (some max/min value) for your loop.
Re: Stock Warning -
Ilhan - 18.02.2016
Quote:
Originally Posted by Gammix
First one appears because its a never ending loop so you'll never get to return -1.
The second one is what compiler expects to get but it doesn't know ots an infinite loop.
To avoid, you'll need a limit (some max/min value) for your loop.
|
Alright, while i was editing the loop with unreachable max value, figured out a new way. Its fixed by now.
Код:
stock stockname(param)
{
new i=1, mp=4, pp=2, total=0, re=-1;
for(;;i++)
{
total += (i * mp) + pp;
if(total >= param)
{
re=i;break;
}
}
return re;
}
Re: Stock Warning -
Nero_3D - 18.02.2016
... just use a do while loop, also use constants and remove useless variables
PHP код:
stock stockname(param) {
const
mp = 4,
pp = 2
;
new
i
;
do {
param -= (++i * mp) + pp;
} while(0 < param);
return i;
}
Re: Stock Warning -
Ilhan - 18.02.2016
Quote:
Originally Posted by Nero_3D
... just use a do while loop, also use constants and remove useless variables
PHP код:
stock stockname(param) {
const
mp = 4,
pp = 2
;
new
i
;
do {
param -= (++i * mp) + pp;
} while(0 < param);
return i;
}
|
Changed mine with yours. Thanks.