Board index » delphi » Why Delete Procedure Won't Work?

Why Delete Procedure Won't Work?

Hi

I am experiencing some problems with my delete function.
Can anyone take a look at it and tell me what's wrong?
The deletion part is working except that if the number keyed in does not
exist, the "Record Not Found" message would not display.

Thank You. (In Advance)

Best Regards

Poh San
Email: ps7577...@pacific.net.sg

Procedure Delete_Record;

Begin
     Valid := False;
     Clrscr;
     GoToxy(30,5);
     Writeln('DELETE RECORDS');
     Repeat
           GoToxy(15,7);
           Clreol;
           Write('Enter IC_No To delete :');
           Readln (Target_IC_No);
           Position := + 1;
           If (length(Target_IC_No) = 8) Then
              Valid := True;
           While (Valid) And (Position <= length(Target_IC_No)) Do
                 Begin
                      If (Target_IC_No[position] in ['A'..'Z']) or
                         (Target_IC_No[position] in ['a'..'z']) or
                         (Target_IC_No[position] in ['0'..'9']) Then
                         position := position + 1
                      Else
                          Valid := False;
                 End;
           If (Valid = False) Then
              Begin
                   GoToxy(30,23);
                   Write('InValid Entry, Enter Any 8 Numbers or
Characters.');
                   GoToxy(30,24);
                   Write('Press Any Key To Continue...');
                   Readkey;
                   GoToxy(30,24);
                   clreol;
                   GoToxy(30,23);
                   clreol;
              End
           Else
               Begin
                    For position := 1 To length(Target_IC_No) Do
                    Target_IC_No[position] :=
upCase(Target_IC_No[position]);
                    GoToxy(38,7);
                    Writeln(Target_IC_No);
               End;
     until (Valid);
     Found := False;
     For Loop := 1 To 20 Do
         If Result[Loop].IC_No = Target_IC_No Then
            Begin
                 Found := True;
                 If Result[Loop].IC_No[1] <> chr (255) Then
                    GoToxy(20,9);
                    Writeln('RECORD FOUND!');
                    GoToxy(20,11);
                    Write('Do you wish To Delete record, Y/N ? >');
      Read(Answer);
      If (Answer = 'Y') or (Answer = 'y') Then
   Begin
                             Clrscr;
                             Result[Loop].IC_No[1] := chr (255);
                             GoToxy(30,13);
                             Write('RECORD DELETED!');
                             GoToxy(45,23);
                             Write('Press Enter To Continue...');
                             Readln;
                        End;
                            If NOT Found Then
                               Begin
                                    Clrscr;
                                    GoToxy(30,13);
                                    Write('RECORD NOT FOUND!');
                                    GoToxy(45,23);
                                    Write('Press Enter To Continue...');
                                    Readln;
                               End;
            End;
End;

 

Re:Why Delete Procedure Won't Work?


Quote
Lim Poh San wrote:

> Hi

> I am experiencing some problems with my delete function.

..> Can anyone take a look at it and tell me what's wrong?

Here's your code..
-> and my comments:

Procedure Delete_Record;

Begin
     Valid := False;
     Clrscr;
     GoToxy(30,5);
     Writeln('DELETE RECORDS');
     Repeat
           GoToxy(15,7);
           Clreol;
           Write('Enter IC_No To delete :');
           Readln (Target_IC_No);
           Position := + 1;
           If (length(Target_IC_No) = 8) Then
              Valid := True;
           While (Valid) And (Position <= length(Target_IC_No)) Do
                 Begin
                      If (Target_IC_No[position] in ['A'..'Z']) or
                         (Target_IC_No[position] in ['a'..'z']) or
                         (Target_IC_No[position] in ['0'..'9']) Then
                         position := position + 1
                      Else
                          Valid := False;
                 End;
           If (Valid = False) Then
              Begin
                   GoToxy(30,23);
                   Write('InValid Entry, Enter Any 8 Numbers or
Characters.');
                   GoToxy(30,24);
                   Write('Press Any Key To Continue...');
                   Readkey;
                   GoToxy(30,24);
                   clreol;
                   GoToxy(30,23);
                   clreol;
              End
           Else
               Begin
                    For position := 1 To length(Target_IC_No) Do
                    Target_IC_No[position] :=
upCase(Target_IC_No[position]);
                    GoToxy(38,7);
                    Writeln(Target_IC_No);
               End;
     until (Valid);
-> My Suggestion: Put the whole Code for entering the IC_No and the
-> validation in a separate function, like
-> function Enter_IC_No:string[8];", giving back a validated IC_No.
-> valid shouldn't be a global variable, declare it local in
Enter_IC_No.

->I don't know how much the indention of your code was scrambled by
-> your/my mailer, but it was not very readible. I think this is
-> something you could improve.
     Found := False;
     For Loop := 1 To 20 Do
        If Result[Loop].IC_No = Target_IC_No Then
           Begin
             Found := True;
             If Result[Loop].IC_No[1] <> chr (255) Then
               GoToxy(20,9);
-> Is this really what you want to do? With your indention it looked
like
-> you wanted the following commands only beeing executed when the above
-> if-then-statement matching. without begin-end only the gotoxy is
executed
             Writeln('RECORD FOUND!');
             GoToxy(20,11);
             Write('Do you wish To Delete record, Y/N ? >');
             Read(Answer);
             If (Answer = 'Y') or (Answer = 'y') Then
               Begin
                 Clrscr;
                 Result[Loop].IC_No[1] := chr (255);
                 GoToxy(30,13);
                 Write('RECORD DELETED!');
                 GoToxy(45,23);
                 Write('Press Enter To Continue...');
                 Readln;
               End;
             If NOT Found Then
-> This couldn't work because this if-statement is inside of the
-> if Result[Loop].IC_No = Target_IC_No then-block. It is only executed
-> when a reord is found (found is set true above)
-> You can avoid such problems with correct indention and comments at
the
-> end of a block
               Begin
                 Clrscr;
                 GoToxy(30,13);
                 Write('RECORD NOT FOUND!');
                 GoToxy(45,23);
                 Write('Press Enter To Continue...');
                 Readln;
               End;
           End; {of if Result[Loop].IC_No = Target_IC_No then }
-> It`s a good idea having comments at the end of a block,
-> saying what this begin-end block is about.
End;{of procedure Delete_Record}

Re:Why Delete Procedure Won't Work?


Quote
Lim Poh San wrote:

> Hi

> I am experiencing some problems with my delete function.
> Can anyone take a look at it and tell me what's wrong?
> The deletion part is working except that if the number keyed in does not
> exist, the "Record Not Found" message would not display.
[...]
>      Found := False;
>      For Loop := 1 To 20 Do
>          If Result[Loop].IC_No = Target_IC_No Then
>            Begin
>               Found := True;
>               If Result[Loop].IC_No[1] <> chr (255) Then
>                     GoToxy(20,9);
>               Writeln('RECORD FOUND!');
>               GoToxy(20,11);
>               Write('Do you wish To Delete record, Y/N ? >');
>               Read(Answer);
>               If (Answer = 'Y') or (Answer = 'y') Then
>                      Begin

                  { delete record }

Quote
>                 End;
>               If NOT Found Then
>                 Begin
>                   Clrscr;
>                   Write('RECORD NOT FOUND!');
>                 End;
>             End;

I've taken the liberty to fix your indentation.  As should be now
obvious, the program only does something when the record is found.  It
should read:

      Found := False;
      For Loop := 1 To 20 Do
         Begin
          If Result[Loop].IC_No = Target_IC_No Then
            Begin
               Found := True;
               If Result[Loop].IC_No[1] <> chr (255) Then
                     GoToxy(20,9);
               Writeln('RECORD FOUND!');
               GoToxy(20,11);
               Write('Do you wish To Delete record, Y/N ? >');
               Read(Answer);
               If (Answer = 'Y') or (Answer = 'y') Then
                  Begin
                  { delete record }
                 End;
             End;
         End;
      If NOT Found Then
        Begin
          Clrscr;
          Write('RECORD NOT FOUND!');
        End;

Hope this helps,
Jochen

Other Threads