Lazarus

Free Pascal => Beginners => Topic started by: daniilprokh on July 31, 2021, 08:06:21 pm

Title: Misunderstanding of procedure val
Post by: daniilprokh 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?
Title: Re: Misunderstanding of procedure val
Post by: Handoko 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.
Title: Re: Misunderstanding of procedure val
Post by: winni 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
Title: Re: Misunderstanding of procedure val
Post by: winni on July 31, 2021, 09:14:10 pm
Hi!

Simple proof:

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

results in err = 1

Winni
Title: Re: Misunderstanding of procedure val
Post by: Kays 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 (http://www.pascal-central.com/iso7185.html#6.1.5%20Numbers) is accepted, and be it just the start, just the sign. Any leading blanks are skipped (https://www.freepascal.org/docs-html/rtl/system/val.html), as it is the case in read/readLn (http://www.pascal-central.com/iso7185.html#6.9.1%20The%20procedure%20read).
TinyPortal © 2005-2018