HELP!! Scariest bug ever
#1

HELP!!
I can't even explain it, here is my code:


Код:
#define LICENSE_PLATE_LENGTH 7
stock GeneratePlate(vehicleid)
{
	new licenseplate[LICENSE_PLATE_LENGTH],
	    numberstr[5], len;
	format(numberstr, 5, "%d", vehicleid);
	len = strlen(numberstr);
 	for(new c; c < LICENSE_PLATE_LENGTH; c++)
 	{
 	    if(c > 1 && c <= 1 + len)
		{
            licenseplate[c] = numberstr[c - 2];
		}
	 	else switch©
		{
		    case 0, 1: licenseplate[c] = random('Z' - 'A') + 'A';
		    default: licenseplate[c] = random('9' - '0') + '0';
		}
	}
	print(licenseplate);
	return licenseplate;
}
Then, try it out by:
Код:
public OnGameModeInit()
{
 	for(new i; i < 100; i++) GeneratePlate(i);
Spent so much time trying to solve this... just can't.
Reply
#2

You go from if to else switch.....

thats not right lol
Reply
#3

That is completely legitimate.
Reply
#4

There is no such thing as:

Else switch

Only:

Else {
switch () {

}
}

Yet i only see one set of brackets and that is a multi line statement.....
Reply
#5

Try it out yourself, the syntax is definitely okay.

Just like you'd do
Код:
if(statement) var++;
instead of
Код:
if(statement)
{
    var++;
}
Reply
#6

Yeah that's for a single line statement only, a lot of languages do that.

A multi line statement is usually stricter with requirement for parenthesis.

The compiler might be allowing it, but I would be concerned as to whether it really works as intended lol.

pawn Код:
if(c > 1 && c <= 1 + len)
        {
            licenseplate[c] = numberstr[c - 2];
        }
        else switch(c)
        {
            case 0, 1: licenseplate[c] = random('Z' - 'A') + 'A';
            default: licenseplate[c] = random('9' - '0') + '0';
        }
Your if has only one line statement but has parenthesis, your else has a switch and 2 statements but no parenthesis..... It could be valid but it looks like a bad habit to get into.

I'll take a look.
Reply
#7

Quote:
Originally Posted by GrimR
Посмотреть сообщение
Yeah that's for a single line statement only, a lot of languages do that.

A multi line statement is usually stricter with requirement for parenthesis.

The compiler might be allowing it, but I would be concerned as to whether it really works as intended lol.

pawn Код:
if(c > 1 && c <= 1 + len)
        {
            licenseplate[c] = numberstr[c - 2];
        }
        else switch(c)
        {
            case 0, 1: licenseplate[c] = random('Z' - 'A') + 'A';
            default: licenseplate[c] = random('9' - '0') + '0';
        }
Your if has only one line statement but has parenthesis, your else has a switch and 2 statements but no parenthesis..... It could be valid but it looks like a bad habit to get into.

I'll take a look.
Alright, I'll do it that way from now on, thanks.
Now please lets go back to my matter.
Reply
#8

Yeah I see, I assume you mean the wacked out characters?

My first thought is..... random() on the SA:MP wiki has no mention of using it in the way that you are lol.

What kind of license plate are you trying to generate (is it just 6 random number/char), it sort of looks like 2 chars and 4/5 numbers AB12345?

I already made on working at the moment that generates 7 the same as above format without any issues.
Reply
#9

Here, this will generate the 7 random num/chars:

pawn Код:
new abc[][26] =
{
  {"A"}, {"B"}, {"C"}, {"D"}, {"E"}, {"F"}, {"G"}, {"H"}, {"I"}, {"J"}, {"K"},
  {"L"}, {"M"}, {"N"}, {"O"}, {"P"}, {"Q"}, {"R"}, {"S"}, {"T"}, {"U"}, {"V"},
  {"W"}, {"X"}, {"Y"}, {"Z"}

};

new rNum[][10] =
{
  {"0"}, {"1"}, {"2"}, {"3"}, {"4"}, {"5"}, {"6"}, {"7"}, {"8"}, {"9"}
};

#define LICENSE_PLATE_LENGTH 7

stock GeneratePlate(vehicleid)
{
  new licenseplate[LICENSE_PLATE_LENGTH];

  for (new c; c < LICENSE_PLATE_LENGTH; c++)
  {
    new rand  = random(sizeof(abc));
    new rand2 = random(sizeof(rNum));

    switch (c)
    {
      case 0, 1: licenseplate[c] = abc[rand][0];
      default  : licenseplate[c] = rNum[rand2][0];
    }
  }
  return licenseplate;
}
And in OnGameModeInit for testing:

pawn Код:
for (new i; i < 100; i++) { print(GeneratePlate(i)); }
Obviously to change length you modify your #DEFINE, and to mix up chars and numbers, modify the case statement accordingly.
Reply
#10

Yeah, the wacked out characters, also the freaky beeping the computer does when launching the server.
I guess I'll pick your way then, thanks.
btw this:
Код:
new abc[][26] =
{
  {"A"}, {"B"}, {"C"}, {"D"}, {"E"}, {"F"}, {"G"}, {"H"}, {"I"}, {"J"}, {"K"},
  {"L"}, {"M"}, {"N"}, {"O"}, {"P"}, {"Q"}, {"R"}, {"S"}, {"T"}, {"U"}, {"V"},
  {"W"}, {"X"}, {"Y"}, {"Z"}

};
is better to be replaced with
Код:
new abc[26] =
{
    'A', 'B', ....
};
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)