### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Misunderstanding of procedure val  (Read 2013 times)

#### daniilprokh

• Newbie
• Posts: 1
##### Misunderstanding of procedure val
« on: July 31, 2021, 08:06:21 pm »
Hello, everyone!

I tested val procedure and notice something:
Code: Pascal  [Select][+][-]
1. var
2.     x : integer;
3.     code : word;
4. begin
5.     val('-', x, code);    {code = 2, length of string = 1}
6.     val('+', x, code);    {code = 2, length of string = 1}
7.     val('\$', x, code)    {code = 2, length of string = 1}
8. end.
9.
I receive position of non-existent character. Is it OK?

#### Handoko

• Hero Member
• Posts: 4327
• My goal: build my own game engine using Lazarus
##### Re: Misunderstanding of procedure val
« Reply #1 on: July 31, 2021, 08:55:41 pm »
Hello daniilprokh,
Welcome to the forum.

My guess, the computer think the string is missing a something (or a number) after the symbol.
'-5'
'+7'
'\$3''

The 5, 7 and 3 are in the second position. That's why the computer said there's something wrong in the position no 2.
« Last Edit: July 31, 2021, 08:57:31 pm by Handoko »

#### winni

• Hero Member
• Posts: 2663
##### Re: Misunderstanding of procedure val
« Reply #2 on: July 31, 2021, 08:59:16 pm »
Hi!

For shure is 2 correct.

'+'  is a prefix for an integer. And an integer is exspected in your code. So he is missing the first digit  of the integer.

The same with '-'

In the case of '\$' an hexadecimal string is exspected to be converted into an integer. Also here a char is missing - here a hexdecimal.

So this is a correct error.

Winni

PS : Handoko again got the Gold Medal
« Last Edit: July 31, 2021, 09:03:18 pm by winni »

#### winni

• Hero Member
• Posts: 2663
##### Re: Misunderstanding of procedure val
« Reply #3 on: July 31, 2021, 09:14:10 pm »
Hi!

Simple proof:

Code: Pascal  [Select][+][-]
1. val ('z',i,err);

results in err = 1

Winni

#### Kays

• Sr. Member
• Posts: 356
• Whasup!?
##### Re: Misunderstanding of procedure val
« Reply #4 on: July 31, 2021, 10:32:48 pm »
[…] I receive position of non-existent character. Is it OK?
Your x is a signed integer, so anything that matches a signed-integer is accepted, and be it just the start, just the sign. Any leading blanks are skipped, as it is the case in read/readLn.
Yours Sincerely
Kai Burghardt