Bounded List - Programmers Heaven

Howdy, Stranger!

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

Categories

Bounded List

neffsneffs Posts: 39Member
I'm try to write a bounded list program but with little luck. All I'm trying to do is add a number to the list the save the list to file. Hope someone will addvise me on where I'm going worng.

Thanks

Neffs

PROGRAM BoundedList (Input,Output);

Const Max = 5;

TYPE itemtype = integer;

listtype = RECORD
L : array [1..Max] of itemtype;
S : integer;
END;

FileType = File of listtype;

VAR Blist : listtype;
I : Integer;
Error : Boolean;
C : Char;

PROCEDURE Initialise(VAR Blist:listtype);

BEGIN
Blist.S :=0;
END;

PROCEDURE Menu;

BEGIN
Writeln(' The function Game');
Writeln('------------------------------');
Writeln(' 1. Add to list');
Writeln('------------------------------');
END;

PROCEDURE Add_to_list(VAR Blist : listtype;VAR Error : Boolean);

VAR New_Item : itemtype;

BEGIN
Write('Please enter your number to be added :- ');
Readln(New_Item);
IF Blist.S = Max THEN
BEGIN
Error := True;
Write('Sorry list full');
END
Else
Error := False;
Blist.S := Blist.S + 1;
Blist.L := Blist.L + New_Item;
END;

PROCEDURE Get_Choice(VAR C : Char);

BEGIN
Write('Please enter choice :- ');
Readln(C);
END;

PROCEDURE Act_On_Choice(C : Char);

BEGIN
CASE C OF
'1' : Add_to_list( Blist,Error);
END;
END;

BEGIN

ASSIGN(Blist,'Blist.dat');
RESET(Blist)
Menu;
Get_Choice(C);
Act_on_Choice(C);

END.

Comments

  • zibadianzibadian Posts: 6,349Member
    : I'm try to write a bounded list program but with little luck. All I'm trying to do is add a number to the list the save the list to file. Hope someone will addvise me on where I'm going worng.
    [code]
    :
    : Thanks
    :
    : Neffs
    :
    : PROGRAM BoundedList (Input,Output);
    :
    : Const Max = 5;
    :
    : TYPE itemtype = integer;
    :
    : listtype = RECORD
    : L : array [1..Max] of itemtype;
    : S : integer;
    : END;
    :
    : FileType = File of listtype;
    :
    : VAR Blist : listtype;
    : I : Integer;
    : Error : Boolean;
    : C : Char;
    :
    : PROCEDURE Initialise(VAR Blist:listtype);
    :
    : BEGIN
    : Blist.S :=0;
    : END;
    :
    : PROCEDURE Menu;
    :
    : BEGIN
    : Writeln(' The function Game');
    : Writeln('------------------------------');
    : Writeln(' 1. Add to list');
    : Writeln('------------------------------');
    : END;
    :
    : PROCEDURE Add_to_list(VAR Blist : listtype;VAR Error : Boolean);
    :
    : VAR New_Item : itemtype;
    :
    : BEGIN
    : Write('Please enter your number to be added :- ');
    : Readln(New_Item);
    : IF Blist.S = Max THEN
    : BEGIN
    : Error := True;
    : Write('Sorry list full');
    : END
    : Else begin { begin forgotten: you want to add a new item if the list count is not equal to maximum }
    : Error := False;
    : Blist.S := Blist.S + 1;
    : Blist.L[blist.S] := Blist.L[BList.S] + New_Item; { L is an array }
    end; { end forgotten: see above }
    : END;
    :
    : PROCEDURE Get_Choice(VAR C : Char);
    :
    : BEGIN
    : Write('Please enter choice :- ');
    : Readln(C);
    : END;
    :
    : PROCEDURE Act_On_Choice(C : Char);
    :
    : BEGIN
    : CASE C OF
    : '1' : Add_to_list( Blist,Error);
    : END;
    : END;
    :
    var
    Blistfile:filetype; { <==== you need a file variable to write to }
    : BEGIN
    :
    :{ ASSIGN(Blist,'Blist.dat'); <==== wrong, Blist is NOT a file, but a record }
    ASSIGN(Blistfile,'Blist.dat');
    : RESET(Blistfile) { <==== forgotten ; }
    { the line above only works if the file "blist.dat" already exists }
    : Menu;
    : Get_Choice(C);
    : Act_on_Choice(C);
    write(Blistfile,BList); { <==== forgotten }
    : close(blist) { <==== forgotten }
    : END.
    :
    [/code]

    Above is you code, with most of the bugs out of it. It should work now, although you can only add one number at present. Good luck.

  • neffsneffs Posts: 39Member
    : : I'm try to write a bounded list program but with little luck. All I'm trying to do is add a number to the list the save the list to file. Hope someone will addvise me on where I'm going worng.
    : [code]
    : :
    : : Thanks
    : :
    : : Neffs
    : :
    : : PROGRAM BoundedList (Input,Output);
    : :
    : : Const Max = 5;
    : :
    : : TYPE itemtype = integer;
    : :
    : : listtype = RECORD
    : : L : array [1..Max] of itemtype;
    : : S : integer;
    : : END;
    : :
    : : FileType = File of listtype;
    : :
    : : VAR Blist : listtype;
    : : I : Integer;
    : : Error : Boolean;
    : : C : Char;
    : :
    : : PROCEDURE Initialise(VAR Blist:listtype);
    : :
    : : BEGIN
    : : Blist.S :=0;
    : : END;
    : :
    : : PROCEDURE Menu;
    : :
    : : BEGIN
    : : Writeln(' The function Game');
    : : Writeln('------------------------------');
    : : Writeln(' 1. Add to list');
    : : Writeln('------------------------------');
    : : END;
    : :
    : : PROCEDURE Add_to_list(VAR Blist : listtype;VAR Error : Boolean);
    : :
    : : VAR New_Item : itemtype;
    : :
    : : BEGIN
    : : Write('Please enter your number to be added :- ');
    : : Readln(New_Item);
    : : IF Blist.S = Max THEN
    : : BEGIN
    : : Error := True;
    : : Write('Sorry list full');
    : : END
    : : Else begin { begin forgotten: you want to add a new item if the list count is not equal to maximum }
    : : Error := False;
    : : Blist.S := Blist.S + 1;
    : : Blist.L[blist.S] := Blist.L[BList.S] + New_Item; { L is an array }
    : end; { end forgotten: see above }
    : : END;
    : :
    : : PROCEDURE Get_Choice(VAR C : Char);
    : :
    : : BEGIN
    : : Write('Please enter choice :- ');
    : : Readln(C);
    : : END;
    : :
    : : PROCEDURE Act_On_Choice(C : Char);
    : :
    : : BEGIN
    : : CASE C OF
    : : '1' : Add_to_list( Blist,Error);
    : : END;
    : : END;
    : :
    : var
    : Blistfile:filetype; { <==== you need a file variable to write to }
    : : BEGIN
    : :
    : :{ ASSIGN(Blist,'Blist.dat'); <==== wrong, Blist is NOT a file, but a record }
    : ASSIGN(Blistfile,'Blist.dat');
    : : RESET(Blistfile) { <==== forgotten ; }
    : { the line above only works if the file "blist.dat" already exists }
    : : Menu;
    : : Get_Choice(C);
    : : Act_on_Choice(C);
    : write(Blistfile,BList); { <==== forgotten }
    : : close(blist) { <==== forgotten }
    : : END.
    : :
    : [/code]
    :
    : Above is you code, with most of the bugs out of it. It should work now, although you can only add one number at present. Good luck.
    :


Sign In or Register to comment.