3

So I was thinking how is it possible to ask for an input of an unknown number of variables on one line only by using a space to separate them... For instance, if the number of variables I want to input is known, the code would be..

Readln(a,b,c)

This would ask for an input of 3 variables, split by a space or by pressing enter after each, but the split by using space is what I am going for. But what if I dont know the number of variables and I need to create them on the go?.. Say, n sets the number of variables I need to input.

readln(n);
( n = 2 )
readln(a,b..... any number of variables equal to n)

Note that the number of variables I read after n need to be equal to the number that n holds, and can not be pre-set in var. I tried different ways of doing this but all I came up with was

readln(n)
for i := 0 to n-1 do
  readln(a[i])

But by using the loop and array, I still can only input one variable on each line, not any number separated by a space. Is there any way to do this?

Jack Avante
  • 1,405
  • 1
  • 15
  • 32

3 Answers3

2

To avoid using some additional classes use Read instead of ReadLn. May be this code will be helpful:

program Project1;

var
    input: array of Integer;
    i, j: Integer;
begin
    SetLength(input, 1000);
    i := 0;
    repeat
        Read(input[i]);
        Inc(i);
    until input[i-1] = 0;

    for j := 0 to i-1 do
        Writeln(input[j]);

    Readln;
    Readln;
end.

And console content (just as example):

1 4 7 9 6 3 6
4 7 3 1 8
9 4 6
0
1
4
7
9
6
3
6
4
7
3
1
8
9
4
6
0

Terminal was a main tool for conversation between Operator and Computer so it can handle almost any types and kinds of input data using some agreements of course :)

Abelisto
  • 14,826
  • 2
  • 33
  • 41
1

You should consider the string with the values a value itself. So you read once, into one variable. Then you process the result, i.e. splitting on spaces and handling any errors.

For example:

var
  ValuesSeparatedBySpaces: string;
  ValueList: TStringList;
  I: Integer;
begin
  // Read all values together
  ReadLn(ValuesSeperatedBySpaces);
  // Create a list to split them into
  ValueList := TStringList.Create;
  try
    // Use the space character and only the space character for splitting
    ValueList.Delimiter := ' ';
    ValueList.StrictDelimiter := True;
    // Do the splitting
    ValueList.DelimitedText := ValuesSeparatedBySpaces;
    // Show the result
    for I := 0 to ValueList.Count - 1 do
      Writeln(ValueList[I]);
  finally
    // Clean up
    LValueList.Free;
  end;

If you need to values of a different type (e.g. Integers) you need to do the conversion (e.g. StrToInt) after building the list of values.

Thijs van Dien
  • 6,516
  • 1
  • 29
  • 48
  • So there is no more options other than this? I have used this method in my last project, sorry for not mentioning, and I thought if there was a simpler way.. But if this is the only way then thanks anyway, I will stick to it. – Jack Avante Sep 20 '15 at 13:59
  • @TiboroJacko I'm not aware of any included function doing exactly what you want, but nothing stops you from writing your own function that does this, not to have to repeat this code. – Thijs van Dien Sep 20 '15 at 14:03
1

Here try with this code,you don't even need to know how exactly variables there is, all you need to know is what type are they( in my example i used integers) and make an array out of them.

program backo;
 var niz:array [1..100] of integer;
  n,  i:integer;

 begin
i:=1;
    writeln('enter elements of array');
    read(niz[i]);
    while not eoln do
            begin
                    i:=i+1;
                    read(niz[i]);
            end;
    for n:=i downto 1 do
    writeln(niz[n]);
 end.

at the end you have the number of variables in 'i', and you get your array written backwards, for example if input is: 1 3 9 3 4 9, your output would be

9

4

3

9

3

1

nikojov
  • 55
  • 9