Jason Madi
Jason Madi

Reputation: 41

Delphi Array: Variable Myvar might not been initialized

I'm work on a program for school (Cinema app) but I have a problem with my array. My app closed but nothing is showed.

    program TFE;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  StrUtils,
  Crt;

var
  MovieList, MovieInfo: Text;
  Choice: Byte;
  i: Integer;
  L: String;
  S: array of String[14];

begin

i := 0
Assign(MovieInfo, 'MovieInfo.txt');
    Reset(MovieInfo);
Readln(Choice);
i := 0;
ClrScr;
    While not eof (MovieInfo) do                  
      begin
        Readln(MovieInfo, L);
        S[i] := L;
        i := i + 1;
      end;
Writeln(S[Choice]);
Readln;

end.

It's all my code for the moment. Somebody can help me ?

Upvotes: 1

Views: 220

Answers (2)

Bogdan Doicin
Bogdan Doicin

Reputation: 2416

The message you got is correct, because you only work with the array inside the while not eof loop. At that moment, the compiler cannot know the content of the file. It may be blank, as far as he's concerned. If the file is, indeed, blank, he'll skip entirely the while not eof part and go straight to the array writing part. Because the array was never used, it doesn't have a defined value, hence this message.

The solution is simple: initialize the array's values with 0:

        program TFE;

        {$APPTYPE CONSOLE}

        uses
          SysUtils,
          StrUtils,
          Crt;

        var
          MovieList, MovieInfo: Text;
          Choice: Byte;
          i: Integer;
          L: String;
          S: array of String[14];

        begin

        SetLength(s,10); //10 is an example
        for i:=0 to Length(s) do
         s[i]:='';
        Assign(MovieInfo, 'MovieInfo.txt');
        Reset(MovieInfo);
        Readln(Choice);
        i := 0;
        ClrScr;
            While not eof (MovieInfo) do                  
              begin
                Readln(MovieInfo, L);
                S[i] := L;
                i := i + 1;
              end;
        Writeln(S[Choice]);
        Readln;       
        end.

Digging deep into your code, you define MovieList and MovieInfo, but you only use MovieInfo. Why?

Upvotes: 0

Tom Brunberg
Tom Brunberg

Reputation: 21033

In the title you speak about a variable MyVar, but the code doesn't show any such variable. For future reference, please carefully proof read your question before posting.

You have declared a dynamic array:

S: array of String[14];

that is, an array of 14 character strings (short strings). But you have never set the length of this array, and so it can not hold any strings at all.

Use procedure SetLength(var S: <string or dynamic array>; NewLength: Integer); to allocate space for items in the array.

As you dont know (I presume) how many movies there might be in the file, you must first allocate some amount, and then be prepared to expand the array (with a new call to SetLength()) if the array becomes filled up before all movies are read from the file. For example, initialize (before the while loop) with space for 10 movies:

SetLength(S, 10);

and then in the while loop, e.g. just before ReadLn(),

if i > (Length(S)-1) then
  SetLength(S, Length(S)+10);

Another comment is that the user is not presented any prompt when requested for a choice, but maybe this is still under development ;-)

Upvotes: 2

Related Questions