开发者

Why this code doesn't works in Pascal?

开发者 https://www.devze.com 2023-01-19 03:34 出处:网络
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]开发者_JS百科, UserInput[3] );

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]开发者_JS百科, 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消