SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 19/01/2017, 12:56 AM   #1
Rongvaldyr
Little Clucker
 
Join Date: Oct 2016
Posts: 12
Reputation: 2
Default Wrong loop result.

Hello, I'm trying to implement vehicle price to dialog title and I'm trying to use loop to get correct price from model id.

I got this codes;

Code:
new VehiclePrices[47][47] =
{
   // {model, price}
   {400, 40000}, //Landstalker
   {600, 44000}, //Picador
   {602, 60500} //Alpha
};

So when a player in vehicle model 400 I want it to show "40000" on dialog title but instead of this it shows "60500" which is for veh model 602.

Here is my looping codes;

Code:
stock ShowVehicleDialog(playerid)
{
	for ( new i = 0; i < sizeof(VehiclePrices); i++ )
    {
	new vid = GetPlayerVehicleID(playerid), model = GetVehicleModel(vid);
	new price = VehiclePrices[i][1];
	new str[256];
	format(str, sizeof(str), "%s %d "COL_DGREEN"$%d", VehicleNames[GetVehicleModel(GetPlayerVehicleID(playerid))-400], model, FormatNumber(price);
	Dialog_Show(playerid, BUYVEHICLE, DIALOG_STYLE_LIST, str,"Test", "SeÁ", "İptal");
	}
	return 1;
}
So, it gets the vehicle name from the vehiclenames correctly but it doesn't loop vehicle price correctly, it starts from the end.

So, how can I fix this code or do you guys have any other suggestions for this.
Rongvaldyr is offline   Reply With Quote
Old 19/01/2017, 04:20 AM   #2
BroZeus
High-roller
 
BroZeus's Avatar
 
Join Date: Jan 2014
Location: India
Posts: 1,398
Reputation: 173
Default Re: Wrong loop result.

First of all change your VehiclePrices declaration to this :
Quote:
new VehiclePrices[47][2]
Then use this code:
PHP Code:
stock ShowVehicleDialog(playerid)
{
    new 
model GetVehicleModel(GetPlayerVehicleID(playerid)), found = -1;
    for ( new 
0sizeof(VehiclePrices); i++ )
    {
        if(
model == VehiclePrices[i][0])
        {
            
found i;
            break;
        }
    }    
    new 
str[100];
    if(
found == -1)str "Model not found";
    
//note the difference in format parameters in next line
    
else format(strsizeof(str), "%s %d "COL_DGREEN"$%d"VehicleNames[model-400], modelFormatNumber(VehiclePrices[found][1]));
    
Dialog_Show(playeridBUYVEHICLEDIALOG_STYLE_LISTstr,"Test""SeÁ""İptal");    
    return 
1;

__________________


"Be the change that you wish to see in the world."

Check out my Github profile!
BroZeus is offline   Reply With Quote
Old 19/01/2017, 04:30 AM   #3
Rongvaldyr
Little Clucker
 
Join Date: Oct 2016
Posts: 12
Reputation: 2
Default Re: Wrong loop result.

I did as you said but unfortunately it didn't work, now it shows $52 for the price


as you can see here: http://i.imgur.com/uh6Pk7n.jpg


but there is no 52 in my prices, my exact declaration for prices is like this :

Code:
new VehiclePrices[47][2] =
{
   // {model, price}
   {400, 40000}, //Landstalker
   {401, 36000}, //Bravura
   {402, 125000}, //Buffalo
   {404, 18000}, //Perenial
   {410, 14500}, //Manana
   {412, 40000}, //VooDoo
   {418, 20000}, //Moonbeam
   {419, 54000}, //Esperanto
   {422, 48000}, //Bobcat
   {436, 35000}, //Previon
   {445, 60000}, //Admiral
   {458, 60000}, //Solair
   {466, 38000}, //Glendale
   {467, 50500}, //Oceanic
   {474, 48000}, //Hermes
   {475, 56000}, //Sabre
   {478, 20000}, //Walton
   {479, 40000}, //Regina
   {482, 54000}, //Burrito
   {489, 52000}, //Rancher
   {491, 46500}, //Virgo
   {492, 46000}, //Greenwood
   {496, 42000}, //BlistaC
   {500, 54000}, //Mesa
   {516, 38000}, //Nebula
   {517, 40500}, //Majestic
   {518, 42000}, //Buccaneer
   {526, 39500}, //Fortune
   {527, 28500}, //Cadrona
   {529, 50000}, //Willard
   {534, 66000}, //Remington
   {536, 54000}, //Blade
   {542, 19000}, //Clover
   {543, 30000}, //Sadler
   {546, 45500}, //Intruder
   {547, 44500}, //Primo
   {549, 19500}, //Tampa
   {551, 44000}, //Merit
   {554, 58000}, //Yosemite
   {566, 48000}, //Tahoma
   {567, 50000}, //Savanna
   {576, 36000}, //Tornado
   {579, 70000}, //Huntley
   {585, 80000}, //Emperor
   {589, 63000}, //Club
   {600, 44000}, //Picador
   {602, 60500} //Alpha
};
Rongvaldyr is offline   Reply With Quote
Old 19/01/2017, 04:35 AM   #4
Rongvaldyr
Little Clucker
 
Join Date: Oct 2016
Posts: 12
Reputation: 2
Default Re: Wrong loop result.

Okay, when I deleted the "formatnumber" it did work and now showing correct but I would like to use my formatnumber stock too it puts "," to numbers like "$40,000"

Anything I could do to fix this?

here is my format number stock

Code:
stock FormatNumber(Float:amount)
{
	new str[16];
	format(str, sizeof(str), "%d", floatround(amount));
	new l = strlen(str);
	if (amount < 0) // -
	{
  		if (l > 4) strins(str, ",", l-3);
		if (l > 7) strins(str, ",", l-6);
		if (l > 10) strins(str, ",", l-9);
	}
	else
	{
		if (l > 3) strins(str, ",", l-3);
		if (l > 6) strins(str, ",", l-6);
		if (l > 9) strins(str, ",", l-9);
	}
	return str;
}
Rongvaldyr is offline   Reply With Quote
Old 19/01/2017, 04:39 AM   #5
Rongvaldyr
Little Clucker
 
Join Date: Oct 2016
Posts: 12
Reputation: 2
Default Re: Wrong loop result.

Oh, okay I managed to fix it, thank you so much.
Rongvaldyr is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wrong Result PowerF Scripting Help 6 20/09/2015 07:31 AM
Only 1 player will be shown from loop result iPrivate Scripting Help 11 27/10/2014 11:48 PM
[Help] MySQL returning wrong result Maxips2 Scripting Help 6 08/09/2012 10:11 PM
What's wrong with this loop? Knappen Scripting Help 2 07/11/2011 03:51 PM
How to stop a for/while loop looping after it founds the first result? DJ-CJ89 Help Archive 4 15/04/2010 11:20 AM


All times are GMT. The time now is 08:46 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.