My stock code stalls out the compiler?
#1

I made this stock, and it stalls out my compiler, I know its this specific code because I removed it and it worked.

Anyone have any insight as to why this is happening?

And if its the way the code is done, any help to clean it up would be appreciated.

P.S.
When the code only had the "Private" and "Specialist" Line in the beginning, it compiled.....

pawn Код:
stock GetRankNameFromExp(playerid)
{
    new rankname[120];
    switch(PData[playerid][pExp])
    {
        case 0 .. 18500 rankname = "Private";
        case 28000 .. 67999 rankname = "Specialist";
        case 68000 .. 119999 rankname = "Corporal";
        case 120000 .. 178999 rankname = "Sergeant";
        case 179000 .. 246999 rankname = "Staff Sergeant";
        case 247000 .. 322999 rankname = "Master Sergeant";
        case 323000 .. 404999 rankname = "First Sergeant";
        case 405000 .. 536999 rankname = "Warrant Officer";
        case 537000 .. 831999 rankname = "Chief Warrant Officer";
        case 832000 .. 1239999 rankname = "Second Lieutenant";
        case 1240000 .. 1755999 rankname = "First Lieutenant";
        case 1756000 .. 2379999 rankname = "Captain";
        case 2380000 .. 31119999 rankname = "Major";
        case 3112000 .. 3951999 rankname = "Lieutenant Colonel";
        case 3952000 .. 4899999 rankname = "Colonel";
        case 4900000 .. 5955999 rankname = "Brigadier General";
        case 5956000 .. 6331999 rankname = "General";
        case 6332000 .. 9999999 rankname = "General of the Army";
        default rankname = "Private";
    }
    return rankname;
}
Reply
#2

pawn Код:
stock GetRank(exp, rank = 0)
{
    if(exp < RankList[0][RankXP])
    {
        return 0;
    }
    if(!(0 <= rank < sizeof RankList) || (exp < RankList[rank][RankXP])) {
        rank = 0;
    }
    while((rank < sizeof RankList) && (RankList[rank][RankXP] <= exp)) rank++;

    return rank - 1;
}

stock GetPlayerRankName(playerid)
{
    GetPlayerRank(playerid);
    // Getting the rank and directly returns the name (without strcat)
    #emit const.alt RankList
    #emit idxaddr
    #emit move.alt
    #emit load.i
    #emit add
    #emit add.c 4
    #emit stor.s.pri 16
    #emit retn
    // That code below \/ also works, /\ that is just faster, code will stop here (after #emit retn)
    // Removing the normal code would break the emit instructions
    new
        tmp[32]
    ;
    strcat(tmp, RankList[GetPlayerRank(playerid)][RankName]);
    return tmp;
}
Thanks to Nero3d, he made this code for me. Change this code, i have made an enum to store the Ranks and this GetRank Function makes a loop and gets its rank.

EDIT: Example Enum=
pawn Код:
Enum pData
{
    RankNo,
    RankName[65],
    MinXp
}
new Rank[][pData] =
{
    {0, "Rookie", 0},
    {1, "Private", 500}
};
Reply
#3

Interesting piece of code, Nero did very well on that one.

However, This little piece of code.
pawn Код:
GetPlayerRank(playerid);
I assume is something else you didnt give me, or is a piece of code that no longer exists...
Reply
#4

Quote:
Originally Posted by [TC]XxJuggaloxX
Посмотреть сообщение
I made this stock, and it stalls out my compiler, I know its this specific code because I removed it and it worked.

Anyone have any insight as to why this is happening?

And if its the way the code is done, any help to clean it up would be appreciated.

P.S.
When the code only had the "Private" and "Specialist" Line in the beginning, it compiled.....

pawn Код:
stock GetRankNameFromExp(playerid)
{
    new rankname[120];
    switch(PData[playerid][pExp])
    {
        case 0 .. 18500 rankname = "Private";
        case 28000 .. 67999 rankname = "Specialist";
        case 68000 .. 119999 rankname = "Corporal";
        case 120000 .. 178999 rankname = "Sergeant";
        case 179000 .. 246999 rankname = "Staff Sergeant";
        case 247000 .. 322999 rankname = "Master Sergeant";
        case 323000 .. 404999 rankname = "First Sergeant";
        case 405000 .. 536999 rankname = "Warrant Officer";
        case 537000 .. 831999 rankname = "Chief Warrant Officer";
        case 832000 .. 1239999 rankname = "Second Lieutenant";
        case 1240000 .. 1755999 rankname = "First Lieutenant";
        case 1756000 .. 2379999 rankname = "Captain";
        case 2380000 .. 31119999 rankname = "Major";
        case 3112000 .. 3951999 rankname = "Lieutenant Colonel";
        case 3952000 .. 4899999 rankname = "Colonel";
        case 4900000 .. 5955999 rankname = "Brigadier General";
        case 5956000 .. 6331999 rankname = "General";
        case 6332000 .. 9999999 rankname = "General of the Army";
        default rankname = "Private";
    }
    return rankname;
}
pawn Код:
stock GetRankNameFromExp(playerid)
{
    new rankname[120];
    switch(PData[playerid][pExp])
    {
        case 0 .. 18500 rankname = "Private";
        case 28000 .. 67999 rankname = "Specialist";
        case 68000 .. 119999 rankname = "Corporal";
        case 120000 .. 178999 rankname = "Sergeant";
        case 179000 .. 246999 rankname = "Staff Sergeant";
        case 247000 .. 322999 rankname = "Master Sergeant";
        case 323000 .. 404999 rankname = "First Sergeant";
        case 405000 .. 536999 rankname = "Warrant Officer";
        case 537000 .. 831999 rankname = "Chief Warrant Officer";
        case 832000 .. 1239999 rankname = "Second Lieutenant";
        case 1240000 .. 1755999 rankname = "First Lieutenant";
        case 1756000 .. 2379999 rankname = "Captain";
        case 2380000 .. 3111999 rankname = "Major";
        case 3112000 .. 3951999 rankname = "Lieutenant Colonel";
        case 3952000 .. 4899999 rankname = "Colonel";
        case 4900000 .. 5955999 rankname = "Brigadier General";
        case 5956000 .. 6331999 rankname = "General";
        case 6332000 .. 9999999 rankname = "General of the Army";
        default rankname = "Private";
    }
    return rankname;
}
I might be wrong, but I think I've seen the problem, try this. (You had a 9 too much at 31119999)

Best regards,
Jesse
Reply
#5

Quote:
Originally Posted by ******
Посмотреть сообщение
There are several problems:

1) You missed the colon after each "case" statement.
2) "swich" actually enumerates EVERY option, "0 .. 10" is just a shortcut for:

pawn Код:
case 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10:
Imagine that, but for your code! Instead, just use "if":

pawn Код:
new rank = PData[playerid][pExp];
if (0 <= rank <= 18500) rankname = "Private";
// ...
Also, what if you are rank 18501 - 27999?
Ah I see, It's 5:36 am for me, I have been up all night coding haha, I'm a bit tired and missed that part.

Yes, the "If" thats exactly what I did after I thought it over a bit.
My Current code:
pawn Код:
stock GetRankNameFromExp(playerid)
{
    new
        rankname[120],
        Exp = PData[playerid][pExp];
    if(Exp >= 0 && Exp <= 27999) { rankname = "Private"; }
    if(Exp >= 28000 && Exp <= 67999) { rankname = "Specialist"; }
    if(Exp >= 68000 && Exp <= 119999) { rankname = "Corporal"; }
    if(Exp >= 120000 && Exp <= 178999) { rankname = "Sergeant"; }
    if(Exp >= 179000 && Exp <= 246999) { rankname = "Staff Sergeant"; }
    if(Exp >= 247000 && Exp <= 322999) { rankname = "Master Sergeant"; }
    if(Exp >= 323000 && Exp <= 404999) { rankname = "First Sergeant"; }
    if(Exp >= 405000 && Exp <= 536999) { rankname = "Warrant Officer"; }
    if(Exp >= 537000 && Exp <= 831999) { rankname = "Chief Warrant Officer"; }
    if(Exp >= 832000 && Exp <= 1239999) { rankname = "Second Lieutenant"; }
    if(Exp >= 1240000 && Exp <= 1755999) { rankname = "First Lieutenant"; }
    if(Exp >= 1756000 && Exp <= 2379999) { rankname = "Captain"; }
    if(Exp >= 2380000 && Exp <= 3111999) { rankname = "Major"; }
    if(Exp >= 3112000 && Exp <= 3951999) { rankname = "Lieutenant Colonel"; }
    if(Exp >= 3952000 && Exp <= 4899999) { rankname = "Colonel"; }
    if(Exp >= 4900000 && Exp <= 5955999) { rankname = "Brigadier General"; }
    if(Exp >= 5956000 && Exp <= 6331999) { rankname = "General"; }
    if(Exp >= 6332000) { rankname = "General of the Army"; }
    else { rankname = "Private"; }
    return rankname;
}
Reply
#6

I had a similar problem using huge numbers in switch case statements.
Then I figured every number is processed individually, making a HUGE switch case block.
In your case, there will be code for checking 10 million numbers, one by one.
It's the same as having 10 million if-statements after eachother.

In this case, using switch-case isn't a good option.
It's better to use if-statements to solve this, as you can simply do
Код:
if (537000 < PData[playerid][pExp] < 831999)
for example.
Reply
#7

After reading everything here I thought of something what would be much better in my opinion, I'll show you;
pawn Код:
#define MAX_RANKS 18

new RankNames[MAX_RANKS][22] = {
{"Private"},
{"Specialist"},
{"Corporal"},
{"Sergeant"},
{"Staff Sergeant"},
{"Master Sergeant"},
{"First Sergeant"},
{"Warrant Officer"},
{"Chief Warrant Officer"},
{"Second Lieutenant"},
{"First Lieutenant"},
{"Captain"},
{"Major"},
{"Lieutenant Colonel"},
{"Colonel"},
{"Brigadier General"},
{"General"},
{"General of the Army"}
};

new RankXP[MAX_RANKS] = {0, 28000, 68000, 120000, 179000, 247000, 323000, 405000, 537000, 832000, 1240000, 1756000, 2380000, 3112000,\
3952000, 4900000, 5956000, 6332000};

stock GetRankNameFromExp(playerid)
{
    new Exp = PData[playerid][pExp];
    for(new i = 0; i < MAX_RANKS; i++)
        {
        if(Exp >= RankXP[i] && Exp < RankXP[i+1]) { return RankNames[i]; }
        else if(i == MAX_RANKS-1 && Exp >= RankXP[MAX_RANKS-1]) { return RankNames[MAX_RANKS-1]; }
        else if(i == MAX_RANKS-1) { return RankNames[0]; }
        }
}
This way you can easily manage ranks (Adding them for example) without too much things to change, just change MAX_RANKS, add a rank name in 'RankNames', add the XP required to get them in 'RankXP' and voilla, done.

Best regards,
Jesse
Reply
#8

I'll show you what all this work is going to

Reply
#9

Quote:
Originally Posted by jessejanssen
Посмотреть сообщение
After reading everything here I thought of something what would be much better in my opinion, I'll show you;
pawn Код:
#define MAX_RANKS 18

new RankNames[MAX_RANKS][22] = {
{"Private"},
{"Specialist"},
{"Corporal"},
{"Sergeant"},
{"Staff Sergeant"},
{"Master Sergeant"},
{"First Sergeant"},
{"Warrant Officer"},
{"Chief Warrant Officer"},
{"Second Lieutenant"},
{"First Lieutenant"},
{"Captain"},
{"Major"},
{"Lieutenant Colonel"},
{"Colonel"},
{"Brigadier General"},
{"General"},
{"General of the Army"}
};

new RankXP[MAX_RANKS] = {0, 28000, 68000, 120000, 179000, 247000, 323000, 405000, 537000, 832000, 1240000, 1756000, 2380000, 3112000,\
3952000, 4900000, 5956000, 6332000};

stock GetRankNameFromExp(playerid)
{
    new Exp = PData[playerid][pExp];
    for(new i = 0; i < MAX_RANKS; i++)
        {
        if(Exp >= RankXP[i] && Exp < RankXP[i+1]) { return RankNames[i]; }
        else if(i == MAX_RANKS-1 && Exp >= RankXP[MAX_RANKS-1]) { return RankNames[MAX_RANKS-1]; }
        else if(i == MAX_RANKS-1) { return RankNames[0]; }
        }
}
This way you can easily manage ranks (Adding them for example) without too much things to change, just change MAX_RANKS, add a rank name in 'RankNames', add the XP required to get them in 'RankXP' and voilla, done.

Best regards,
Jesse
I like it, It's nice and easily changeable, however, it throws warnings.

pawn Код:
warning 209: function "GetRankNameFromExp" should return a value
Reply
#10

I have no clue what this post is doing here, please delete it.

Best regards,
Jesse
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)