Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

help with records Please help!!

joelem18joelem18 Posts: 31Member
[b][red]This message was edited by Moderator at 2003-4-24 6:50:14[/red][/b][hr]
I am doing a student's database and i have to display a record concerned and change its data if necessary. I have to use the arrow keys to move between fields.

I have no clue how to change the data. Please help

Here's what i've done so far


[code]
program database;

uses crt;

type

subs1 = set of 0..8;

student_record = record
Surname : String[2];
Name : String[10];
subs : subs1;
end;

subj_record = record
studrecno : integer;
grade : array[1..3] of integer;
end;

index_record = record
studrecno : integer;
Surname : String[2];
Name : String[10];
end;


const
MaxNoOfStudents = 30;
Subject_array : array [1..8] of string[4] = ('Math', 'Phys', 'SOK', 'Bio', 'Chem', 'Comp', 'Eng', 'Span');

var
mainfile : file of student_record;
subjfile : file of subj_record;
indexfile : file of index_record;
onestud : student_record;
onesubj : subj_record;
oneidx : index_record;





Procedure InitDatafiles;
Begin

assign(mainfile, 'C:main.DAT');
rewrite(mainfile);
close(mainfile);

assign(indexfile,'C:index.IDX');
rewrite(indexfile);
reset(indexfile);
end;

Procedure OpenSubjFile(Subjno : integer);
{opens a subject file associated with its set number SubjNo for the global variable subjfile,
Adds suffix '.DAT to the file name}
var
s : string[8];
Begin
s := subject_array[SubjNo] + '.DAT';
Assign(subjfile, s);
Reset(subjfile);
End;


Procedure GenerateRecords(newrecs : integer);
var
i, j, k, l : integer;
tempsur : string[2];
tempname : string[10];
tempsubs : subs1;
subno, fsize, tsubno, hgrade, ygrade, avggrade : integer;

Begin

tsubno := 1;

with onestud do
begin
Surname := '';
Name := '';
subs := [];
end;

with onesubj do
begin
studrecno := 0;
end;

with oneidx do
begin
studrecno := 0;
surname := '';
name := '';
end;

assign(mainfile, 'C:main.DAT');
reset(mainfile);
For i := 1 to newrecs do
begin
tempsur := chr(65 + Random(26));
for j := 2 to 4 do
tempsur := tempsur + chr(65 + 32 + Random(26));

{A = 65, B = 66, C = 67, ...)
(small letters = values of capital + 32}

tempname := chr(Random(26));
for k := 2 to 10 do
tempname := tempname + chr(Random(26));

for l := 1 to 3 do
begin
repeat
subno := 1 + random(8);
until not (subno in tempsubs);
tempsubs := tempsubs + [subno];
end;

onestud.surname := tempsur;
onestud.name := tempname;
onestud.subs := tempsubs;
seek(mainfile, filesize(mainfile));
write(mainfile, onestud);
fsize := filesize(mainfile) - 1;
close(mainfile);


oneidx.studrecno := fsize;
oneidx.name := onestud.name;
oneidx.surname := onestud.surname;
assign(indexfile, 'C:index.IDX');
reset(indexfile);
seek(indexfile, filesize(indexfile));
write(indexfile, oneidx);
close(indexfile);

onesubj.studrecno := oneidx.studrecno;
while tempsubs <> [] do
begin
if tsubno in tempsubs then
begin
opensubjfile(tsubno);

hgrade := 1 + random(100);
ygrade := 1 + random(100);
avggrade := (hgrade + ygrade) div 2;

onesubj.grade[1] := onesubj.grade[hgrade];
onesubj.grade[2] := onesubj.grade[ygrade];
onesubj.grade[3] := onesubj.grade[avggrade];

write(subjfile, onesubj);
close(subjfile);
tempsubs := tempsubs - [tsubno];
end;
inc(tsubno);
end;

end;
[/code]

I haven't tried doing the menu yet since this changing of data is my first priority
Thanks




[Red]*Edited By Moderator To Add Formatting Tags[/Red]
«1

Comments

  • joelem18joelem18 Posts: 31Member
    i have compiled the program successfully but when i ran the program to generate some records, it stopped at the opensubjfile procedure in the reset(subjfile) line. Can any1 tell me what the problem is?
    Thanks
  • zibadianzibadian Posts: 6,349Member
    : i have compiled the program successfully but when i ran the program to generate some records, it stopped at the opensubjfile procedure in the reset(subjfile) line. Can any1 tell me what the problem is?
    : Thanks
    :
    The file specified didn't exists perhaps due to a typo in either the subject name or the filename; or perhaps an illegal character was in the filename.
  • joelem18joelem18 Posts: 31Member
    Please can someone help me with the problem i have with records that i mentioned before?

  • ibyiby Posts: 323Member
    : i have compiled the program successfully but when i ran
    : the program to generate some records, it stopped at the
    : opensubjfile procedure in the reset(subjfile) line.
    : Can any1 tell me what the problem is?
    : Thanks


    It's hard to say based on portion of the program you have posted
    but let's take a look at the procedure you mention:

    [code]Procedure OpenSubjFile(Subjno : integer);
    var
    s : string[8];
    Begin
    s := subject_array[SubjNo] + '.DAT';
    Assign(subjfile, s);
    Reset(subjfile);
    End;[/code]

    It looks to me that when you try to open file ("reset(subjfile)")
    Pascal faults because file with that name doesn't exist in
    currect directory. Why don't you put a breakpoint on "Assign..."
    line and run the program again. Once it stops there, open
    watches window and check what is the file name you try to open
    and what is the value of SubjNo.
    Common mistakes are double extension for example ("file.DAT.DAT")
    or missing file name (".DAT").
  • zibadianzibadian Posts: 6,349Member
    : : i have compiled the program successfully but when i ran
    : : the program to generate some records, it stopped at the
    : : opensubjfile procedure in the reset(subjfile) line.
    : : Can any1 tell me what the problem is?
    : : Thanks
    :
    :
    : It's hard to say based on portion of the program you have posted
    : but let's take a look at the procedure you mention:
    :
    : [code]Procedure OpenSubjFile(Subjno : integer);
    : var
    : s : string[8];
    : Begin
    : s := subject_array[SubjNo] + '.DAT';
    : Assign(subjfile, s);
    : Reset(subjfile);
    : End;[/code]
    :
    : It looks to me that when you try to open file ("reset(subjfile)")
    : Pascal faults because file with that name doesn't exist in
    : currect directory. Why don't you put a breakpoint on "Assign..."
    : line and run the program again. Once it stops there, open
    : watches window and check what is the file name you try to open
    : and what is the value of SubjNo.
    : Common mistakes are double extension for example ("file.DAT.DAT")
    : or missing file name (".DAT").
    :
    The name file.dat.dat cannot exist because the complete file name (including extension) cannot be longer than 8 characters (see string declaration). Perhaps that is the problem, the extension might be truncated away.
  • joelem18joelem18 Posts: 31Member
    Ok i fixed some of the code but i still have the same problem. I tried using breakpoints and watches. They were quite useful but i still didn't find out why it gives me the file not found error. I checked that this error comes up when the computer generates 2 or more records. Here's the code. The program is set to generate one record.

    program database;

    uses crt;

    type

    subs1 = set of 0..8;

    student_record = record
    Surname : String[2];
    Name : String[10];
    subs : subs1;
    end;

    subj_record = record
    studrecno : integer;
    grade : array[1..3] of integer;
    end;

    index_record = record
    studrecno : integer;
    Surname : String[2];
    Name : String[10];
    end;


    const
    MaxNoOfStudents = 30;
    Subject_array : array [1..8] of string[4] = ('Math', 'Phys', 'SOK', 'Bio', 'Chem', 'Comp', 'Eng', 'Span');

    var
    mainfile : file of student_record;
    subjfile : file of subj_record;
    indexfile,tempidx : file of index_record;
    onestud : student_record;
    onesubj : subj_record;
    oneidx : index_record;





    Procedure InitDatafiles;
    Begin

    assign(mainfile, 'C:main.DAT');
    rewrite(mainfile);
    close(mainfile);

    assign(indexfile,'C:index.IDX');
    rewrite(indexfile);
    close(indexfile);
    end;


    Procedure OpenSubjFile(Subjno : integer);
    {opens a subject file associated with its set number SubjNo for the global variable subjfile,
    Adds suffix '.DAT to the file name}
    var
    s : string[12];
    Begin
    s :='C:' + subject_array[SubjNo] + '.DAT';
    Assign(subjfile, s);
    Reset(subjfile);
    End;


    Procedure GenerateRecords(newrecs : integer);
    var
    i, j, k, l : integer;
    tempsur : string[3];
    tempname : string[10];
    tempsubs : subs1;
    subno, fsize, tsubno, hgrade, ygrade, avggrade : integer;

    Begin
    tempsubs := [];
    tsubno := 1;

    with onestud do
    begin
    Surname := '';
    Name := '';
    subs := [];
    end;

    with onesubj do
    begin
    studrecno := 0;
    end;

    with oneidx do
    begin
    studrecno := 0;
    surname := '';
    name := '';
    end;

    assign(mainfile, 'C:main.DAT');
    reset(mainfile);
    For i := 1 to newrecs do
    begin
    tempsur := '';
    tempname := '';
    randomize;
    tempsur := chr(65 + Random(26));
    for j := 2 to 3 do
    tempsur := tempsur + chr(65 + 32 + Random(26));

    {A = 65, B = 66, C = 67, ...)
    (small letters = values of capital + 32}
    randomize;
    tempname := chr(97 + Random(26));
    for k := 2 to 10 do
    tempname := tempname + chr(65 + 32 + Random(26));

    for l := 1 to 3 do
    begin
    repeat
    subno := 1 + random(8);
    until not (subno in tempsubs);
    tempsubs := tempsubs + [subno];
    end;

    onestud.surname := tempsur;
    onestud.name := tempname;
    onestud.subs := tempsubs;
    assign(mainfile, 'C:main.DAT');
    reset(mainfile);
    seek(mainfile, filesize(mainfile));
    write(mainfile, onestud);
    fsize := filesize(mainfile);
    close(mainfile);


    oneidx.studrecno := fsize;
    oneidx.name := onestud.name;
    oneidx.surname := onestud.surname;
    assign(indexfile, 'C:index.IDX');
    reset(indexfile);
    seek(indexfile, filesize(indexfile));
    write(indexfile, oneidx);
    close(indexfile);

    onesubj.studrecno := oneidx.studrecno;
    while tempsubs <> [] do
    begin
    if tsubno in tempsubs then
    begin
    opensubjfile(tsubno);

    hgrade := 1 + random(100);
    ygrade := 1 + random(100);
    avggrade := (hgrade + ygrade) div 2;

    onesubj.grade[1] := onesubj.grade[hgrade];
    onesubj.grade[2] := onesubj.grade[ygrade];
    onesubj.grade[3] := onesubj.grade[avggrade];

    write(subjfile, onesubj);
    close(subjfile);
    tempsubs := tempsubs - [tsubno];
    end;
    inc(tsubno);
    end;

    end;
    end;

    Procedure Lister;
    var
    temprec1,temprec2,temprec3,temptemp,temptemptemp : index_record;
    flogger : boolean;
    k,j,z,finalint,kr : integer;

    begin
    Assign(tempidx,'c: empidx.idx');
    Rewrite(tempidx);
    Close(tempidx);
    Repeat
    Assign(indexfile,'c:index.idx');
    Reset(indexfile);
    for k := 0 to filesize(indexfile) - 1 do
    begin
    flogger := false;
    seek(indexfile, k);
    read(indexfile, temprec1);
    If k + 1 < filesize(indexfile) then
    Begin
    seek(indexfile, k + 1);
    read(indexfile, temprec2);
    if temprec1.surname > temprec2.surname then
    begin
    flogger := true;
    assign(tempidx,'c: empidx.idx');
    Reset(tempidx);
    If k > 0 then
    Begin
    For j := 0 to k - 1 do
    begin
    seek(indexfile, j);
    read(indexfile, temprec3);
    seek(tempidx, filesize(tempidx));
    write(tempidx, temprec3);
    end;
    end;
    seek(tempidx, filesize(tempidx));
    Write(tempidx, temprec2);
    Write(tempidx, temprec1);
    For j := k + 2 to filesize(indexfile) - 1 do
    Begin
    seek(indexfile,j);
    read(indexfile,temprec3);
    seek(tempidx, filesize(tempidx));
    write(tempidx, temprec3);
    end;
    close(tempidx);
    close(indexfile);
    Assign(indexfile,'c:index.idx');
    Rewrite(indexfile);
    Reset(indexfile);
    assign(tempidx,'c: empidx.idx');
    reset(tempidx);
    for z := 0 to filesize(tempidx) - 1 do
    begin
    seek(tempidx, z);
    read(tempidx, temptemp);
    write(indexfile, temptemp);
    end;
    rewrite(tempidx);
    reset(tempidx);
    close(tempidx);
    End;
    end
    else
    begin
    reset(indexfile);
    close(indexfile);
    end;
    end;
    Until flogger = false;

    Assign(indexfile,'c:index.idx');
    Reset(indexfile);
    Textbackground(blue); Textcolor(yellow);
    Clrscr;
    Gotoxy(35,4); Write('Student List');
    kr := 0;
    for finalint := 0 to filesize(indexfile) - 1 do
    begin
    kr := Kr + 1;
    Read(indexfile, temprec3);
    gotoxy(25,8 + kr);
    Write(temprec3.Surname);
    gotoxy(45,8+kr);
    Write(temprec3.Name);
    end;
    close(indexfile);
    repeat until keypressed;

    end;

    Begin
    initdatafiles;
    generaterecords(1);
    lister;

    readln
    end.
  • ibyiby Posts: 323Member
    : Ok i fixed some of the code but i still have the same problem. I tried using breakpoints and watches. They were quite useful but i still didn't find out why it gives me the file not found error. I checked that this error comes up when the computer generates 2 or more records. Here's the code. The program is set to generate one record.
    :
    : program database;
    :
    : uses crt;
    :
    : type
    :
    : subs1 = set of 0..8;
    :
    : student_record = record
    : Surname : String[2];
    : Name : String[10];
    : subs : subs1;
    : end;
    :
    : subj_record = record
    : studrecno : integer;
    : grade : array[1..3] of integer;
    : end;
    :
    : index_record = record
    : studrecno : integer;
    : Surname : String[2];
    : Name : String[10];
    : end;
    :
    :
    : const
    : MaxNoOfStudents = 30;
    : Subject_array : array [1..8] of string[4] = ('Math', 'Phys', 'SOK', 'Bio', 'Chem', 'Comp', 'Eng', 'Span');
    :
    : var
    : mainfile : file of student_record;
    : subjfile : file of subj_record;
    : indexfile,tempidx : file of index_record;
    : onestud : student_record;
    : onesubj : subj_record;
    : oneidx : index_record;
    :
    :
    :
    :
    :
    : Procedure InitDatafiles;
    : Begin
    :
    : assign(mainfile, 'C:main.DAT');
    : rewrite(mainfile);
    : close(mainfile);
    :
    : assign(indexfile,'C:index.IDX');
    : rewrite(indexfile);
    : close(indexfile);
    : end;
    :
    :
    : Procedure OpenSubjFile(Subjno : integer);
    : {opens a subject file associated with its set number SubjNo for the global variable subjfile,
    : Adds suffix '.DAT to the file name}
    : var
    : s : string[12];
    : Begin
    : s :='C:' + subject_array[SubjNo] + '.DAT';
    : Assign(subjfile, s);
    : Reset(subjfile);
    : End;
    :
    :
    : Procedure GenerateRecords(newrecs : integer);
    : var
    : i, j, k, l : integer;
    : tempsur : string[3];
    : tempname : string[10];
    : tempsubs : subs1;
    : subno, fsize, tsubno, hgrade, ygrade, avggrade : integer;
    :
    : Begin
    : tempsubs := [];
    : tsubno := 1;
    :
    : with onestud do
    : begin
    : Surname := '';
    : Name := '';
    : subs := [];
    : end;
    :
    : with onesubj do
    : begin
    : studrecno := 0;
    : end;
    :
    : with oneidx do
    : begin
    : studrecno := 0;
    : surname := '';
    : name := '';
    : end;
    :
    : assign(mainfile, 'C:main.DAT');
    : reset(mainfile);
    : For i := 1 to newrecs do
    : begin
    : tempsur := '';
    : tempname := '';
    : randomize;
    : tempsur := chr(65 + Random(26));
    : for j := 2 to 3 do
    : tempsur := tempsur + chr(65 + 32 + Random(26));
    :
    : {A = 65, B = 66, C = 67, ...)
    : (small letters = values of capital + 32}
    : randomize;
    : tempname := chr(97 + Random(26));
    : for k := 2 to 10 do
    : tempname := tempname + chr(65 + 32 + Random(26));
    :
    : for l := 1 to 3 do
    : begin
    : repeat
    : subno := 1 + random(8);
    : until not (subno in tempsubs);
    : tempsubs := tempsubs + [subno];
    : end;
    :
    : onestud.surname := tempsur;
    : onestud.name := tempname;
    : onestud.subs := tempsubs;
    : assign(mainfile, 'C:main.DAT');
    : reset(mainfile);
    : seek(mainfile, filesize(mainfile));
    : write(mainfile, onestud);
    : fsize := filesize(mainfile);
    : close(mainfile);
    :
    :
    : oneidx.studrecno := fsize;
    : oneidx.name := onestud.name;
    : oneidx.surname := onestud.surname;
    : assign(indexfile, 'C:index.IDX');
    : reset(indexfile);
    : seek(indexfile, filesize(indexfile));
    : write(indexfile, oneidx);
    : close(indexfile);
    :
    : onesubj.studrecno := oneidx.studrecno;
    : while tempsubs <> [] do
    : begin
    : if tsubno in tempsubs then
    : begin
    : opensubjfile(tsubno);
    :
    : hgrade := 1 + random(100);
    : ygrade := 1 + random(100);
    : avggrade := (hgrade + ygrade) div 2;
    :
    : onesubj.grade[1] := onesubj.grade[hgrade];
    : onesubj.grade[2] := onesubj.grade[ygrade];
    : onesubj.grade[3] := onesubj.grade[avggrade];
    :
    : write(subjfile, onesubj);
    : close(subjfile);
    : tempsubs := tempsubs - [tsubno];
    : end;
    : inc(tsubno);
    : end;
    :
    : end;
    : end;
    :
    : Procedure Lister;
    : var
    : temprec1,temprec2,temprec3,temptemp,temptemptemp : index_record;
    : flogger : boolean;
    : k,j,z,finalint,kr : integer;
    :
    : begin
    : Assign(tempidx,'c: empidx.idx');
    : Rewrite(tempidx);
    : Close(tempidx);
    : Repeat
    : Assign(indexfile,'c:index.idx');
    : Reset(indexfile);
    : for k := 0 to filesize(indexfile) - 1 do
    : begin
    : flogger := false;
    : seek(indexfile, k);
    : read(indexfile, temprec1);
    : If k + 1 < filesize(indexfile) then
    : Begin
    : seek(indexfile, k + 1);
    : read(indexfile, temprec2);
    : if temprec1.surname > temprec2.surname then
    : begin
    : flogger := true;
    : assign(tempidx,'c: empidx.idx');
    : Reset(tempidx);
    : If k > 0 then
    : Begin
    : For j := 0 to k - 1 do
    : begin
    : seek(indexfile, j);
    : read(indexfile, temprec3);
    : seek(tempidx, filesize(tempidx));
    : write(tempidx, temprec3);
    : end;
    : end;
    : seek(tempidx, filesize(tempidx));
    : Write(tempidx, temprec2);
    : Write(tempidx, temprec1);
    : For j := k + 2 to filesize(indexfile) - 1 do
    : Begin
    : seek(indexfile,j);
    : read(indexfile,temprec3);
    : seek(tempidx, filesize(tempidx));
    : write(tempidx, temprec3);
    : end;
    : close(tempidx);
    : close(indexfile);
    : Assign(indexfile,'c:index.idx');
    : Rewrite(indexfile);
    : Reset(indexfile);
    : assign(tempidx,'c: empidx.idx');
    : reset(tempidx);
    : for z := 0 to filesize(tempidx) - 1 do
    : begin
    : seek(tempidx, z);
    : read(tempidx, temptemp);
    : write(indexfile, temptemp);
    : end;
    : rewrite(tempidx);
    : reset(tempidx);
    : close(tempidx);
    : End;
    : end
    : else
    : begin
    : reset(indexfile);
    : close(indexfile);
    : end;
    : end;
    : Until flogger = false;
    :
    : Assign(indexfile,'c:index.idx');
    : Reset(indexfile);
    : Textbackground(blue); Textcolor(yellow);
    : Clrscr;
    : Gotoxy(35,4); Write('Student List');
    : kr := 0;
    : for finalint := 0 to filesize(indexfile) - 1 do
    : begin
    : kr := Kr + 1;
    : Read(indexfile, temprec3);
    : gotoxy(25,8 + kr);
    : Write(temprec3.Surname);
    : gotoxy(45,8+kr);
    : Write(temprec3.Name);
    : end;
    : close(indexfile);
    : repeat until keypressed;
    :
    : end;
    :
    : Begin
    : initdatafiles;
    : generaterecords(1);
    : lister;
    :
    : readln
    : end.
    :

  • ibyiby Posts: 323Member
    i've never had problem with breakpoints.
    obviously you have problem that your program
    generates filename(s) that do not exist.
    try logging things into a text file or
    add following code just after assign and
    before reset statement:

    [code]
    writeln('S=',s);
    readln;
    [/code]

    this way you would have a chance to take a look at the
    filename before atempt to open file...
  • zibadianzibadian Posts: 6,349Member
    [code]
    : Procedure OpenSubjFile(Subjno : integer);
    : {opens a subject file associated with its set number SubjNo for the global variable subjfile,
    : Adds suffix '.DAT to the file name}
    : var
    : s : string[red][12][/red];
    : Begin
    : s :='C:' + subject_array[SubjNo] + '.DAT';
    : Assign(subjfile, s);
    : Reset(subjfile);
    : End;
    [/code]
    Perhaps you should also drop the fixed length for your filenames, or make that large enough for any name including its path. In DOS this was about 79 characters, if memory serves me well.
  • Phat NatPhat Nat Posts: 757Member
    : [code]
    : : Procedure OpenSubjFile(Subjno : integer);
    : : {opens a subject file associated with its set number SubjNo for the global variable subjfile,
    : : Adds suffix '.DAT to the file name}
    : : var
    : : s : string[red][12][/red];
    : : Begin
    : : s :='C:' + subject_array[SubjNo] + '.DAT';
    : : Assign(subjfile, s);
    : : Reset(subjfile);
    : : End;
    : [/code]
    : Perhaps you should also drop the fixed length for your filenames, or make that large enough for any name including its path. In DOS this was about 79 characters, if memory serves me well.


    Good point Zibadian. If VAR [b]s[/b] can only handle 12 characters, that's okay for a complete filename ( FILENAME.DAT ) but you are assigning an extension to it as well as the root ( 'C:' & '.DAT' ) Therefore, if the filename is 8 letters long ( such as FILENAME ) then you would only end up with the first 12 characters:
    -> C:FILENAME.
    No extension on this one. It's okay to make your VAR [b]subject_array[/b] strings only handle 8 letters (that's the max length of a filename without it's extension anyways), but you may as well just make [b]s:String;[/b]

    Wish I could help more, but I have absolutely no time right now.

    Phat Nat

«1
Sign In or Register to comment.