PDA

View Full Version : Check to see if something is in an array


jeffery30162
14/05/2014, 09:57 PM
I am creating a vehicle thing. I need to check to see if the players vehicle that they are in is on an array. please help me

this is what i have so far.

array:
new RandomRegularVehicle[][3] =
{
// Positions, (model, low price, high price)
{400, 20000, 21000},
{401, 18000, 19000},
{404, 18000, 19000},
{405, 19000, 20000},
{410, 16000, 17000},
{412, 21000, 22000},
{418, 14000, 15000},
{419, 15000, 16000},
{421, 17000, 18000},
{422, 22000, 23000},
{426, 20000, 21000},
{436, 16000, 17000},
{439, 21000, 22000},
{445, 19000, 20000},
{458, 21000, 22000},
{466, 23000, 24000},
{467, 23000, 24000},
{474, 25000, 26000},
{475, 21000, 22000},
{479, 19000, 20000},
{489, 23000, 24000},
{491, 17000, 18000},
{492, 18000, 19000},
{500, 24000, 26000},
{505, 22000, 24000},
{507, 18000, 21000},
{516, 19000, 22000},
{517, 16000, 20000},
{518, 17000, 21000},
{526, 20000, 24000},
{527, 20000, 24000},
{529, 18000, 22000},
{533, 24000, 27000},
{534, 19000, 23000},
{535, 25000, 32000},
{536, 24000, 31000},
{540, 19000, 22000},
{542, 16000, 21000},
{543, 20000, 24000},
{545, 24000, 30000},
{546, 19000, 22000},
{547, 19000, 22000},
{549, 17000, 22000},
{550, 23000, 26000},
{551, 21000, 26000},
{554, 25000, 34000},
{558, 24000, 26000},
{560, 28000, 37000},
{561, 23000, 35000},
{562, 27000, 35000},
{565, 32000, 37000},
{566, 19000, 24000},
{567, 24000, 32000},
{575, 22000, 26000},
{576, 21000, 24000},
{579, 26000, 33000},
{580, 21000, 24000},
{585, 21000, 26000},
{587, 32000, 43000},
{589, 27000, 35000},
{600, 23000, 31000},
{602, 32000, 42000}
};

Check to see if its on there:
if(CarInfo[idx][cModel] != RandomRegularVehicle[][0]) return SendClientMessage(playerid, COLOR_RED, "You cannot sell this type of vehicle here");

please help me

Madd92
14/05/2014, 10:32 PM
if(CarInfo[idx][cModel] != RandomRegularVehicle[][0]) return SendClientMessage(playerid, COLOR_RED, "You cannot sell this type of vehicle here");
Do that in a for loop for the max vehicle count.

jeffery30162
14/05/2014, 10:46 PM
Im confused, could you show me what your talking about?

Madd92
14/05/2014, 11:09 PM
for (new i = 0; i < sizeof(RandomRegularVehicle); i++)
if(CarInfo[idx][cModel] != RandomRegularVehicle[i][0]) return SendClientMessage(playerid, COLOR_RED, "You cannot sell this type of vehicle here");

Vince
15/05/2014, 08:32 AM
That is incorrect. If the first vehicle doesn't match, it will return the error without even checking the rest of the array. Never return inside a loop unless you explicitly want to break it.

Madd92
15/05/2014, 08:31 PM
That is incorrect. If the first vehicle doesn't match, it will return the error without even checking the rest of the array. Never return inside a loop unless you explicitly want to break it.
Sorry, you are correct of course. You'd have to break the loop when there is a match. Then after the loop you'd have to check if the index is the size of the array. If not, there was a match so you can sell it, if it is the maximum, you can't.
new i;
for (i = 0; i < sizeof(RandomRegularVehicle); i++)
if(CarInfo[idx][cModel] == RandomRegularVehicle[i][0]) break;
if (i == sizeof(RandomRegularVehicle))
return SendClientMessage(playerid, COLOR_RED, "You cannot sell this type of vehicle here");

jeffery30162
15/05/2014, 10:55 PM
That didn't work either.

Jefff
16/05/2014, 12:45 AM
bool:IncorrectModel(modelid)
{

for(new i=0; i != sizeof(RandomRegularVehicle); i++)
if(RandomRegularVehicle[i][0] == modelid)
return true;

return false;
}

if(!IncorrectModel(CarInfo[idx][cModel])) return SendClientMessage(playerid, COLOR_RED, "You cannot sell this type of vehicle here");

jeffery30162
17/05/2014, 06:05 PM
That didn't work either

Threshold
17/05/2014, 11:37 PM
new bool:modelfound = false;
for(new i = 0; i < sizeof(RandomRegularVehicle); i++)
{
if(CarInfo[idx][cModel] != RandomRegularVehicle[i][0]) continue;
modelfound = true;
break;
}
if(!modelfound) return SendClientMessage(playerid, COLOR_RED, "You cannot sell this type of vehicle here");