http://www.freewebs.com/kimmorleykiller/AT3.rar

Hi all, im pretty new to programming and am having a little trouble with this is a task ive been set to keep track of payments been paid to employees working at a papershop.

Its working well except for the 'Add employee' and 'Delete employee' procedures. I was hoping someone could help me out with those?

Also, ive got a lot of wasted code. Any input on how i could tidy it up a bit would be greatly appreciated.

Cheers guys, KK

nb. Is there anyway way to scroll vertically in pascal programs? im having trouble fitting more than one record on the screen...

You didn't say what kind of Pascal you're using. You didn't tell us how you store your data (array, list, text file, database...). And we can't comment on wasted code without seeing any code. So, please give a few more details!

Sorry guys,

Im using turbo pascal; and am using a text file to store data.

I uploaded the whole thing because its starting to get a little roomy, but here is a procedure im having trouble with:

procedure addemployee;

   begin
      assign(EmployeeFile, 'C:\SDDBooks.txt');
      reset(EmployeeFile);
      Append(EmployeeFile);

      writeln('Please enter new employees identification number: ');
      readln(temp_id);
      writeln(EmployeeFile, temp_id);
      writeln;

      writeln('Please enter employees name: ');
      readln(newspaper[temp_id].employee);
      writeln(EmployeeFile, newspaper[temp_id].employee);
      writeln;

      clearfile;

      writeln('Are these details correct? [Y/N]');
      readln(decision);

   if (decision = 'y') or (decision = 'Y') then
      begin
           close(EmployeeFile);
      end
   else if (decision = 'n') OR (decision = 'N') then
      begin
         clearscreen;
         welcome;
         listoptions;
      end
   else
      begin
           writeln('Invalid input. Employee not saved.');
      end

   end;

This works but its got a lot of bugs in it, is there a way i can get the id number to automatically increase by one from the previous id number in the text file?

Sorry guys,

Im using turbo pascal; and am using a text file to store data.

I uploaded the whole thing because its starting to get a little roomy, but here is a procedure im having trouble with:

procedure addemployee;
 
   begin
      assign(EmployeeFile, 'C:\SDDBooks.txt');
      reset(EmployeeFile);
      Append(EmployeeFile);
 
      writeln('Please enter new employees identification number: ');
      readln(temp_id);
      writeln(EmployeeFile, temp_id);
      writeln;
 
      writeln('Please enter employees name: ');
      readln(newspaper[temp_id].employee);
      writeln(EmployeeFile, newspaper[temp_id].employee);
      writeln;
 
      clearfile;
 
      writeln('Are these details correct? [Y/N]');
      readln(decision);
 
   if (decision = 'y') or (decision = 'Y') then
      begin
           close(EmployeeFile);
      end
   else if (decision = 'n') OR (decision = 'N') then
      begin
         clearscreen;
         welcome;
         listoptions;
      end
   else
      begin
           writeln('Invalid input. Employee not saved.');
      end
 
   end;

This works but its got a lot of bugs in it, is there a way i can get the id number to automatically increase by one from the previous id number in the text file?

You could either save your ID or determine the highest one used to date. Since you're using a text file, it could be saved by itself in the first line where you could find it easily. Be sure to write it back again at the end of the session.

As for the rest, it looks OK, but clearFile seems to be something from your code. What does it do? Also, it looks like you are using an array named newspaper and you're saving the employee name there indexed on the temp_id. Is that correct? Why are you doing that?

Anyway, with all that in mind, I can see several places things could go wrong:

  • You are not making sure that the user is entering a number. You would be better off to generate your own ID as mentioned above anyway, but at least make sure they enter a number and it is within a range you can use.
  • Assuming newspaper is an array, is it big enough to handle any number entered in temp_id?
  • Your overall structure is incorrect. You are writing data to your file before you ask the user if it is correct. You need to ask for confirmation first. Also, if you make a loop to ask the user for confirmation, you can force them to try again if they don't type an acceptable response without making them enter the data all over again. Finally, if you make an outer loop, the user can enter several records in one session. You should probably include a test to make sure the data is valid before you ask the user to confirm it. See if this pseudocode gives you any ideas...good luck
open file(s)
  initialize ID variable
 
  repeat //outer loop to allow several records to be entered 
    user enters data
    test data
    if data is bad then
       display error information //and drop to the last part of this loop   
    else begin
      repeat //inner loop to force user to enter Y or N
        ask user to enter Y or N to confirm the data
        if Y entered then begin  //write the data here
          incriment ID variable
          write data
        end
        else if not N entered then //whatever they entered is wrong
          signal invalid input 
      until user enters Y or N //end inner loop
    end //if data is good
 
    ask user if he/she wants to exit
  until user signals exit //end outer loop
 
  save value of ID variable
  close file(s)
This article has been dead for over six months. Start a new discussion instead.