tags:

views:

74

answers:

2

I have this var:

var
 UserInput : array [1..3] of string;

I'm trying to set multiple values, at once.

readln( UserInput[1], UserInput[2], UserInput[3] );

When the code runs, all the input is stored in UserInput[1]

Ex.:
Input: 10 1.5 18

Result:
UserInput[1] = 10 1.5 18
UserInput[2] = 0
UserInput[3] = 0

What should I do?

+4  A: 

define as float or int not as string:

Var
    myVar   : Integer;
    myArray : Array[1..5] of Integer;

Begin
 myArray[2] := 25;
 myVar := myArray[2];
End.
Sergio Rodriguez
Ok it works. But how can I prevent bad input?
Acacio Nerull
I was using val(), but this statement fails, since UserInput[1] isn't a string anymore
Acacio Nerull
On this level, afaik there is only $I+/- and IOError to check for errors. For more elaborate errorhandling you'll have to read it as string and do your own processing. How easy that is, depends on the compiler. Delphi and FPC have quite nice facilities for this.
Marco van de Voort
+3  A: 

readln just reads text, it doesn't know that you meant "10 1.5 18" to be three different things. To your human eyes, those are three numbers, but to the computer, it's just a nine character string.

My Pascal is very rusty, but if you define UserInput to be of type float, then readln should interpret the text as a number, as you expect. Or, if readln only reads strings, you will have to write more code to convert it to a number.

benzado