Pascal programming help needed please??

Associate
Joined
11 Nov 2006
Posts
549
Location
Devon
Ok i have an assigment using pascal!

The outline of the assignment is

You are to take the role of a secretary whose society requires a method of keeping their monthly/annual statistics on disc in a bespoke program format.

I have done a basic program in pascal which is below.

program TCSP;

Uses Crt;

VAR MemberFile:Text;
Count:INTEGER;
Name:String;

PROCEDURE NamesIn;
BEGIN
Rewrite (MemberFile);
FOR Count:=1 TO 20 DO
BEGIN
ClrScr;
WriteLn('Please enter member name',Count);
ReadLn(Name);
WriteLn(MemberFile,'Member',Count,'is:',name);
END;
Close(MemberFile);
ClrScr;
WriteLn('Member File Created.. press key to end.');
ReadKey;
END;
PROCEDURE ShowNames;
BEGIN
Reset(MemberFile);
WHILE NOT Eof(MemberFile)DO
BEGIN
ReadLn(MemberFile, Name);
WriteLn(Name);
END
WriteLn('Member File Listed..press key to end.');
ReadKey;
END;

BEGIN
Assign(MemberFile,'c:\FileName');
NAmesIn;
ShowNames;
end.

After entering the member name at the very beginning i want the program to ask if new details are to be entered or to look for an old member. I am unsure of how to do this and the code needed to actually look into the memberfile and locate the name.

Any help with this or some polishing up of the current program would be greatly appreciated!

Thanks in advance
 
Hi Byte thanks for your reply!

For the input spec all i need is a member name and their monthly contribution

The output spec is for total monthly contributions and possibly member names too.

Here is the flow chat of what i want to happen:


flow.jpg
 
Hi

Sorry I've been rather busy so haven't gotten around to this earlier.

Can you be a bit more specific about where you're getting stuck? We can't/shouldn't write the assignment for you. That said, here's a template/skeleton that helps with the overall flow (as per your flowchart):

Code:
program TCSP;

Uses Crt;

{ Get a member name from the user and return it as result}
function InputMemberName : String;
var s : String;
begin
  Write('Enter member name:');
  repeat
    ReadLn( s );
    if s = '' then WriteLn('Please enter a name, you cannot enter a blank string.');
  until s <> '';
  InputMemberName := s;
end;

{ Finds out if the user want to enter a new record or view an old one }
function WantToInputNewData : Boolean;
var
  Key : Char;
begin
  Write('Do you want to enter new data or view existing/old data for this name (press N or O)');
  repeat
    Key := ReadKey;
  until Key in ['n', 'o'];
  WantToInputNewData := Key = 'n';
  WriteLn;
end;

{ Input new information and store in file.
  - Should check to see if member already exists }
procedure InputNewDataAndStore(AMemberName : string);
begin
  WriteLn('not implemented');
end;

{ Retrieve named member from file and display information on them }
procedure RetrieveOldDataAndOutput(AMemberName : string);
begin
  WriteLn('not implemented');
end;

{ Find out if the user wants to process another member and returns true if so.}
function WantToProcessAnotherMember : Boolean;
var
  Key : Char;
begin
  Write('Do you want to process another member? (Y/N)');
  repeat
    Key := ReadKey
  until Key in ['y', 'n'];
  WantToProcessAnotherMember := Key = 'y';
  WriteLn;
end;

{ ====================== Main Program =================== }
var
  MemberName : String;
BEGIN
  { The main program reflects the overall program control flow
    as outlined in the flowchart of the program. }
  ClrScr;
  WriteLn('Membership Master V0.1');
  WriteLn('======================');
  repeat
    MemberName := InputMemberName;
    if WantToInputNewData then
      InputNewDataAndStore(MemberName)
    else
      RetrieveOldDataAndOutput(MemberName);
  until not WantToProcessAnotherMember;
end.

Ask when you have more questions.
 
Hi Byte thanks for your reply and your code.

I'm getting stuck on the procedure for the retrieving old member details and the procedure for the amounts to show on screen and be written to the memberfile.txt

My program functions at the moment but only displays the new members names at the end of the program. I want it to display the member names and the amounts.
 
Btw, must your data file be a text file? Can you not make it a file of records, or have you not covered that yet? (Have you done records yet?) Can you post just the code for your "write user to file" procedure? If you don't have one, I suggest you write a procedure to do that. Something like one with a signature like:

Code:
procedure WriteMember(MemberFile : Text; Name : string; amt : Integer);
begin
  {code to write entry to file}
  {pseudocode: 
     1. Find if entry exists already
     1.1 If so, then update entry
     1.2 If not, then create new entry.
  }
end;

You would probably have a converse routine somewhere for reading a single member:


Code:
function ReadMember(MemberFile : Text; var Name : string; var amt : Integer) : Boolean;
begin
  {code to search for entry in file and populate Name and amt variables}
  {pseudocode:
    1. Find if entry exists
    2. If so, then populate variables, and return true
    3. If not, then return false.
   }
 
The procedure used for writing member names to the text file is under PROCEDURE : NamesIN

Line : WriteLn(MemberFile,'Member',Count,'is:',name);

The pascal i am covering at the moment is being taught on my Access to HE course, i believe we go into more detail during the degree.
 
shadow4509 said:
The procedure used for writing member names to the text file is under PROCEDURE : NamesIN

Line : WriteLn(MemberFile,'Member',Count,'is:',name);

The pascal i am covering at the moment is being taught on my Access to HE course, i believe we go into more detail during the degree.

OK. Have you done Records and Files of Records then or not? I presume not?

Re writing member names: I'm suggesting actually working with one member's data at a time, and not reading the entire lot in at once as you appear to currently do. So you'd actually write a Procedure (meaning, a pascal Procedure) to do that single thing: Write a single member to the data file, and possibly another to read a specified member from the data file. The rest of your app can then be blissfully unaware of the file and can just read and write members using these Procedures.

<lecture mode>
Part of the art of programming is breaking your code apart into small, well defined, cohesive parts that may be easily tested and re-used. Long procedures/functions should be avoided, as should routines with more than one main responsibility. Such code then isn't really cohesive anymore, and one will quickly find it hard to understand, debug, test and maintain.
</lecture mode>

The way I see it, your NamesIn has 2 broad responsibilities:
- Dealing with the data file (creating/writing to it/closing it)
- Capturing input from the user (20 times only?? What if there's 10 or 21?)
I would consider it better if this responsibilities was split between 2 or more Procedures.
 
Last edited:
Back
Top Bottom