filesplitlinepascal

Pascal splitting line into real and strings


I know that this language have died a couple of years ago, but still required in most of schools in our country -.- I got file with data, which looks like:

For e.g.

And I need to create result file, which looks like this:

For e.g.

I'm stuck in trying to split the line into string and real, even in the book I've given in the examples all data is on separate line:

I can't find anything on the net and hope you could help me. Thank you in advance.


Solution

  • This should get you started - I got as far as reading the file, splitting the line, and converting the strings to reals:

    Program Test;
    
    var
        fileVar: Text;
        l: string[81];
        inputFilename: string[14];
        lCount: Integer;
        i: Integer;
        code: Integer;
    
        spacePos: Integer;
    
        firstName: string[100];
        secondName: string[100];
    
        num1: real;
        num2: real;
        product: real;
    
        s: string[100];
    
    begin
        inputFilename := 'input.txt';
        Assign(fileVar, inputFilename);
        Reset(fileVar);
    
        Readln(fileVar, l);
        Val(l, lCount);
    
        Writeln('l count=', lCount);
    
        for i := 1 to lCount do
        begin
            Readln(fileVar, l);
            spacePos := Pos(' ', l);
            firstName := Copy(l, 0, spacePos);
            Delete(l, 1, spacePos);
    
            spacePos := Pos(' ', l);
            secondName := Copy(l, 0, spacePos);
            Delete(l, 1, spacePos);
    
            spacePos := Pos(' ', l);
            s := Copy(l, 0, spacePos - 1);
            Val(s, num1, code);
            Delete(l, 1, spacePos);
    
            Val(l, num2, code);
    
            WriteLn(firstName);
            Writeln(secondName);
            Writeln(num1);
            Writeln(num2);
        end;
    
        Close(fileVar);
    end.