need urgent help!! - Programmers Heaven

Howdy, Stranger!

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

Categories

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.

need urgent help!!

joelem18joelem18 Posts: 31Member
I have tried my best to solve my amend record problem and i failed to do so.

I have to do the following
The record concerned is displayed and any of its data can be changed. use arrow keys to move between the fields displayed. The data is to be valdiated before changes take place.

This problem took me over a week to try and solve but i didn't manage. If any1 can solve this problem, I'd be very very grateful

here's the program. To generate some records just click on the queries button. Then go on edit and then amend.

program database;

uses crt;

type

subs1 = set of 1..8;

student_record = record
studrecno : integer;
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 = ('Math', 'Phys', 'SOK', 'Bio', 'Chem', 'Comp', 'Eng', 'Span');
sub_menu : array [0..2] of string = ('Name', 'Surname', 'Subjects');

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;
bew, opvragen : integer;
ant1, choice : char;



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;
Begin
s :='C:' + subject_array[SubjNo] + '.DAT';
Assign(subjfile, s);
{writeln('S=', s);}
readln;
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
subno := 0;
tempsur := '';
tempname := '';
tempsubs := [];
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;
onestud.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;

Procedure chooser;
var selected : integer;
begin
repeat
repeat
textbackground(0);
textcolor(15); textbackground(9);
gotoxy(20, 10); writeln('Student name');
gotoxy(20, 11); writeln('Student surname');
gotoxy(20, 12); writeln('Subjects');
gotoxy(20, selected + 10);
if selected = 3 then gotoxy(20, selected + 10);
textbackground(red); textcolor(yellow);
case selected of
0 : write ('Student name');
1 : write ('Student surname');
2 : write ('Subjects');
end;

textbackground(9); textcolor(15);
textbackground(0);
choice := readkey;
case choice of
#72 : begin
if selected > 0 then
begin
selected := selected - 1;
end
else if selected = 0 then selected := 2;
end;
#80 : begin
if selected < 2 then
begin
selected := selected + 1;
end
else if selected = 2 then selected := 2;
end;

#27 : exit;

end;

until choice = #13;

case selected of
0 : gotoxy(40,40);
1 : gotoxy(40,40);
2 : gotoxy(40,40);
end;

until selected = 3;
end;

Procedure Amend;
var
i, ts, selected, x : integer;
choicer : char;
done : boolean;

begin
selected := 0;
done := False;
clrscr;
ts := 1;
assign(mainfile, 'C:main.dat');
reset(mainfile);
for i := 1 to filesize(mainfile) do
begin
ts := 1;
reset(mainfile);
seek(mainfile, filesize(mainfile) - i);
read(mainfile, onestud);
gotoxy(30, 5);
Writeln(onestud.name);
gotoxy(30, 8);
Writeln(onestud.surname);
gotoxy(30, 11);

while onestud.subs <> [] do
begin
if ts in onestud.subs then
begin
write(subject_array[ts], ' ');
onestud.subs := onestud.subs - [ts];
end;
inc(ts);
end;

choicer := readkey;
case choicer of
#72 : begin
writeln('Entering chooser');
readln;
chooser;

end;
end;

{ repeat
done := false;
For X := 0 to 2 Do
Begin
GotoXY(20,X*3+5);
TextBack
ground(Byte(Selected=X));
TextColor(14 + Byte(Selected=X));
WriteLn(sub_menu[X]);
End;
choice := Readkey;
if choice = #27 then done := true;

if choice = #13 then

if choice = #0 then
begin
choice := readkey;
case choice of
#72 : begin
if selected > 0 then
begin
selected := selected - 1;
end
else if selected = 0 then selected := 2;
end;
#80 : begin
if selected < 2 then
begin
selected := selected + 1;
end
else if selected = 2 then selected := 2;
end;

end;
end;


until Done; }


readln;
close(mainfile);
end;
end;

Procedure Mainmenu1;
begin
repeat
repeat
textbackground(0);
clrscr;
textcolor (15); textbackground(9);
gotoxy (20,5); Writeln (' ? ');
gotoxy (20,6); writeln (' ***** Edit Mode ***** ');
gotoxy (20,7); writeln (' ? ');
gotoxy (20,8); writeln (' Press ESC to return to main menu ');
gotoxy (20,9); writeln (' K ');
gotoxy (20,10);writeln (' Amend ');
gotoxy (20,11);writeln (' Append ');
gotoxy (20,12);writeln (' ? ');
gotoxy (20,bew+10);
if bew = 2 then gotoxy (2,bew+10);
write (' ');
textbackground(red); textcolor(yellow);
case bew of
0 : write (' Amend ');
1 : write (' Append ');
end;

textbackground (9); textcolor (15);
writeln('');
textbackground (0);
ant1 := readkey;
case ant1 of
#72 : begin
if bew > 0 then
begin bew := bew - 1;
end
else if bew = 0 then bew := 2;
end;
#80 : begin
if bew < 2 then
begin bew:=bew+1;
end
else if bew = 2 then bew := 2;
end;

#27 : exit;

end;
until ant1 = #13;

case bew of

0 : Amend;

1 : Lister;


end;


until bew = 2;



end;

Procedure Mainmenu;
begin
repeat
repeat
textbackground(0);
clrscr;
textcolor (15); textbackground(9);
gotoxy (20,5); Writeln (' ? ');
gotoxy (20,6); writeln (' ***** Student Database Menu ***** ');
gotoxy (20,7); writeln (' ? ');
gotoxy (20,8); writeln (' Please press (',chr(24),chr(25),'): ');
gotoxy (20,9); writeln (' K ');
gotoxy (20,10);writeln (' Edit ');
gotoxy (20,11);writeln (' Queries ');
gotoxy (20,12);writeln (' Quit Program ');
gotoxy (20,13);writeln (' ? ');
gotoxy (20,bew+10);
if bew = 3 then gotoxy (3,bew+10);
write (' ');
textbackground(red); textcolor(yellow);
case bew of
0 : write (' Edit ');
1 : write (' Queries ');
2 : write (' Quit Program ');
end;

textbackground (9); textcolor (15);
writeln('');
textbackground (0);
ant1 := readkey;
case ant1 of
#72 : begin
if bew > 0 then
begin bew := bew - 1;
end
else if bew = 0 then bew := 3;
end;
#80 : begin
if bew < 3 then
begin bew:=bew+1;
end
else if bew = 3 then bew := 3;
end;
end;
until ant1 = #13;

case bew of

0 : mainmenu1;

1 : generaterecords(2);

2 : if opvragen < 1 then
begin
bew := 3;
end;
end;

until bew = 3;
end;


Begin
initdatafiles;
mainmenu

end.

Comments

  • roland48roland48 Posts: 23Member
    : I have tried my best to solve my amend record problem and i failed to do so.
    :
    : I have to do the following
    : The record concerned is displayed and any of its data can be changed. use arrow keys to move between the fields displayed. The data is to be valdiated before changes take place.
    :
    : This problem took me over a week to try and solve but i didn't manage. If any1 can solve this problem, I'd be very very grateful
    :
    : here's the program. To generate some records just click on the queries button. Then go on edit and then amend.
    :
    : program database;
    :
    : uses crt;
    :
    : type
    :
    : subs1 = set of 1..8;
    :
    : student_record = record
    : studrecno : integer;
    : 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 = ('Math', 'Phys', 'SOK', 'Bio', 'Chem', 'Comp', 'Eng', 'Span');
    : sub_menu : array [0..2] of string = ('Name', 'Surname', 'Subjects');
    :
    : 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;
    : bew, opvragen : integer;
    : ant1, choice : char;
    :
    :
    :
    : 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;
    : Begin
    : s :='C:' + subject_array[SubjNo] + '.DAT';
    : Assign(subjfile, s);
    : {writeln('S=', s);}
    : readln;
    : 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
    : subno := 0;
    : tempsur := '';
    : tempname := '';
    : tempsubs := [];
    : 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;
    : onestud.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;

    Sorry, I had to cut your source for posting an answer!

    rewrite(tempidx);
    reset(tempidx);
    close(tempidx);

    This code makes no sense. With rewrite you make a new file and
    the existing data are lost.
    Reset opens the file without loosing datat, bat in this case the
    file is already open and a new one.
    Maybe this is the bug!



  • joelem18joelem18 Posts: 31Member
    Thanks for the help but my actual problem is the switching between fields using the arrow keys. I don't know how to do that. I got samples of how to do them but i would appreciate if someone can do it using my coding. All i want is that i prompt the user to if he wants to change the data fields, if not, the program will read the next record and ask the same question. If the user wants to change the data, then a selection between the fields is enabled and if the user selects let's say student name, the data the user will write will overwrite the original.

    I've tried my hardest but i didn't manage, if someone can help me i'd be very grateful
Sign In or Register to comment.