Convert string with numbers into numbers only?
#1

I have this string:

pawn Код:
new pInfo[5], buff[5], string[24];

for(new i; i < 5; i++)
{
     pInfo[i] = "You have +200 points";
     points[i] += 5;
     buff[i] = strval(pInfo[i]);
     printf("Points %i %d", i, buff[i]);
     //more code...
}
And it did not work, I did some debugging and found that strval(); is not working if it has letters..

Thanks
Reply
#2

It's not sending the a message, use SendClientMessage.
Reply
#3

I just need to take the value, not send a message.
Reply
#4

pawn Код:
new pInfo[5], buff[5], string[24];

for(new i; i < 5; i++)
{
     pInfo[i] = "You have +200 points";
     points[i] += 5;
     buff[i] = strval(pInfo[i]);
     printf("Points %i %d", i, buff[i]);
     //more code...
}
Well there's several problems with the code already...like this

pawn Код:
pInfo[i] = "You have +200 points";
You can't store all of those characters in a single cell. I'm quite confused as to what this piece of code is supposed to do...

Really it should be initialized as a multi-dimensional array to store all of those characters.

pawn Код:
new pInfo[5][15];
Anyway as for your question, strval won't extract the numbers from a string, it'll give you the integer value of it (the ASCII value, as far as I know). If you wanted to extract numbers from a string, you'd probably need a function that loops through each character in the string and checking if it's a number or not, then storing it. I'm unaware of an existing function that does that.
Reply
#5

Well, yea, that was just an example, I'm making a top 5 command, and I save the persons in a file with their name and score, "MyName 500", and I need to take "500" from the file.. I used:

pawn Код:
File: hsfile = fopen("/Top5/Top5.ini", io_read);
fread(hsfile, tmp, 128);
buff = strval(tmp);
But didn't work..
Reply
#6

Well that won't work like I said, since strval will just give you the integer value of the string, not extract numbers from it.

Although you could use a split function, to split the string by spaces, a rough example using sscanf by ******.

pawn Код:
new string[2][10], score;
File: hsfile = fopen("/Top5/Top5.ini", io_read);
fread(hsfile, tmp, 128);
sscanf(tmp,"s[10]s[10]",string[0],string[1]);

printf("MyName: %s || MyScore: %d",string[0],strval(string[1]));
You could also use sscanf to immediately store the score as an integer, like this:

pawn Код:
sscanf(tmp,"s[10]d",string,score);
Reply
#7

Yep, works, also, when I save, I can use the same method right?
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)