Loop an enum -
Dayrion - 02.06.2017
Hey.
I'm trying to loop through an enum. The problem is, I can't do it correctly because I've a string + integer.
My enum:
PHP код:
enum E_FACTION_INFOS
{
fSQLID,
fName[MAX_FAC_RANK_NAME],
fDiminutive[MAX_DIMINUTIVE_LT],
fType,
fNumberOfRank,
fMaxMembers,
fPay,
fBank,
fState,...
};
new f_Info[MAX_ALL_FACT][E_FACTION_INFOS];
for(new i = 1, E_FACTION_INFOS:k = fName; i < max_fields_per_table[STATS_INFOS]; i++, k++)
{
mysql_format(db, sec_query, sizeof(sec_query), ", `%s` = '%s'", SQL_Fields[0][i][sqlName], SQL_Fields[0][i][sqlSpecifier]);
printf("[debug-A] %s", sec_query);
mysql_format(db, sec_query, sizeof(sec_query), sec_query, f_Info[factionid][k]);
printf("[debug-B] %s\n", sec_query);
strcat(query, sec_query);
}
It simply print as : f_Info[factionid][fName][0], f_Info[factionid][fName][1], ... instead of f_Info[factionid][fName], f_Info[factionid][fDiminutive].
NB: The loop works correctly apart from the string values
Second question: 4D arrays cost a lot of memory or there any changes?
Edit: I can't use an another dimensions in the loop.
Re: Loop an enum -
CheezIt - 03.06.2017
Simply do:
Re: Loop an enum -
Dayrion - 03.06.2017
Quote:
Originally Posted by CheezIt
|
Did you even read the code entierly ... ?
Re: Loop an enum -
SyS - 03.06.2017
incrementing string is out of logic (k++).That will print characters from beginning.For eg (Samp ,amp,mp,p)
If that's not what you getting then provide a proper test code to reproduce the same problem for analysing.
Re: Loop an enum -
Dayrion - 03.06.2017
Quote:
Originally Posted by SyS
incrementing string is out of logic (k++).That will print characters from beginning.For eg (Samp ,amp,mp,p)
If that's not what you getting then provide a proper test code to reproduce the same problem for analysing.
|
Quote:
Originally Posted by Dayrion
It simply print as : f_Info[factionid][fName][0], f_Info[factionid][fName][1], ... instead of f_Info[factionid][fName], f_Info[factionid][fDiminutive].
NB: The loop works correctly apart from the string values
|
^ That should answer you.
Re: Loop an enum -
GhostHacker9 - 03.06.2017
Quote:
Originally Posted by Dayrion
^ That should answer you.
|
Huh? How could that answer him? Read what he posted carefully.You are incrementing string which will set the pointer from begginning to each character.Also provide proper test code as SyS said to reproduce the problem and analyse as your script is known only to you others even don't saw it before and also you are not clear enough in explaining the actual problem.
Re: Loop an enum -
Dayrion - 04.06.2017
Quote:
Originally Posted by GhostHacker9
Huh? How could that answer him? Read what he posted carefully.You are incrementing string which will set the pointer from begginning to each character.Also provide proper test code as SyS said to reproduce the problem and analyse as your script is known only to you others even don't saw it before and also you are not clear enough in explaining the actual problem.
|
Alright. So I don't understand what he meant.
Quote:
Originally Posted by SyS
incrementing string is out of logic (k++)
|
It's not for the string but it's for the enum.
Quote:
Originally Posted by SyS
If that's not what you getting then provide a proper test code to reproduce the same problem for analysing.
|
I already did that before posting there.
Actually I'm looping from fType (3) to fState(8 ). It works perfectly. The "k" is incrementing my enum!
If I'm starting from fName(1), "k" will increment the string and not the enum.
So I'm looking for a solution because I want "k" incrementing the enum. E_FACTION_INFOS tag on "k" change nothing.
I hope this more clear. Apologize if is it not. My english is pretty bad.
Re: Loop an enum -
CheezIt - 04.06.2017
Quote:
Originally Posted by Dayrion
Did you even read the code entierly ... ?
|
Or maybe you're just ignorant?
This: E_FACTION_INFOS:k = fName - is garbage, simply set k to an integer value where you want it to start looping at
You'll then do:
f_Info[factionid][E_FACTION_INFOS:k]
Re: Loop an enum -
SyS - 04.06.2017
Quote:
Originally Posted by Dayrion
Alright. So I don't understand what he meant.
It's not for the string but it's for the enum.
I already did that before posting there.
Actually I'm looping from fType (3) to fState(8 ). It works perfectly. The "k" is incrementing my enum!
If I'm starting from fName(1), "k" will increment the string and not the enum.
So I'm looking for a solution because I want "k" incrementing the enum. E_FACTION_INFOS tag on "k" change nothing.
I hope this more clear. Apologize if is it not. My english is pretty bad.
|
you are assigning k to a string value that will create corrupted memory as you incrementing it. What you now doing is looping through that string's memory location. The solution above posted is the correct way to do it.
Re: Loop an enum -
SyS - 04.06.2017
Yes instead of that use this (provided from above test code posted)
PHP код:
#include <a_samp>
enum ENUMERATOR
{
value,
string[24]
}
new ARRAY[][ENUMERATOR]={
{1,"Ghost"},
{2,"dariyon"},
{3,"kalcor"}
}
;
main()
{
for(new i = 0; i < 3;++i)
{
printf("string %s \n", ARRAY[i][ENUMERATOR:1]);
printf("digit %d \n ",ARRAY[i][ENUMERATOR:0]);
}
}
Prints
Код:
string Ghost
digit 1
string dariyon
digit 2
string kalcor
digit 3
Hope that was clear to you @op
Also order is an important thing here.As the data type is to be known (array or plane).