Hi,

For a homework assignement, I'v been asked to write a small school administration system.

Currently, the user is able to enter a new student and their age which is then stored in a text file.

This all works fine but now I need to load the contents of the text file into an array where the data can then be represented and used.

The code is written below

program school2;
USES CRT,SYSUTILS;






procedure loaddata;
Begin
End;






procedure enterstudent;
VAR
name:string;
age:integer;
currentfile:text;
Begin

     CLRSCR;
     writeln('Student Name: ');
     readln(name);
     writeln('Student Age: ');
     readln(age);

             Assign(currentfile, 'C:\Users\lavinia\documents\studentdatabase\studentdatabase.txt');
             Append(currentfile);
             writeln(currentfile, name, ',', age);
             Close(currentfile);

End;


procedure setup;
VAR
choice:char;
currentfile:text;
Begin
CLRSCR;
writeln('Welcome to setup....');
writeln('Would you like to setup/restart a new student database (y/n)');
readln(choice);
               if choice = 'y'
               then begin
               Assign(currentfile,'C:\Users\lavinia\Documents\studentdatabase\studentdatabase.txt');
               rewrite(currentfile);
               close(currentfile);
               end
               else begin
               end;
End;





procedure mainmenu;
VAR
choice:char;
Begin
repeat
CLRSCR;
writeln('1) Setup');
writeln('2) New Student');
writeln('3) Statistics');
writeln('4) Exit');
readln(choice);
     case choice of
     '1':setup;
     '2':enterstudent;
     '3':;
     end;
until choice = '4';
End;



begin
loaddata;
mainmenu;
end.

The procedure for the data to be loaded in is called loaddata and has already been put in. Could somebody please help me write this procedure?

Many Thanks for your efforts,

Ed

Although not writing this procedure for you, I can give you a few hints.

First a tip :)
you are working with the "old style" of data storing and reading.
It could be better if you actually made a typed record of your needed information.
Afterwards, easier to read or write individual records from/to disk instead of like now, you have to read or write from beginning to end of file.

Anyway. The commands for reading from file is the same as reading from the Console.
You just use READLN in order to read a whole line in your textfile
your assign command is ok :-) Assign(currentfile, ............
Now consider this use of 'currentfile' :-)
READLN(currentfile,Name);
READLN(currentfile,Age);

As for putting information into an array, I would strongly suggest making this as a typed record due to much better filehandling abilities later.
The only advantage I can see about keeping it as a pure textfile is that you can open the file with notepad and look at and also edit stored data very easily.

Consider:
TYPE
persona = RECORD
Name : STRING;
Age : BYTE; // No person to my knowledge can be older than 255 years.
END; // you must however convert textinput into number. StrToInt() is your command for this.

VAR
Persons : ARRAY[0..100] OF Persona;
DataFile: FILE OF Persona;


Other useful commands to you would be SEEK.
Example :
SEEK(DataFile,0) // positioning the read or write to the first record in your file.
SEEK(DataFile,7) // similar, but now 8th record in your file.

http://delphi.about.com/od/fileio/a/fileof_delphi.htm
has got more information and I am sure you manage.
Good luck, and if this is too advanced, then please write a note about it and I will see if I can find time to make you some better proper examples.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.