Recent

Author Topic: Help Me  (Read 5953 times)

frozensiswa

  • Newbie
  • Posts: 1
Help Me
« on: April 25, 2016, 03:39:55 pm »
Hi can someone help me with my program, i was trying to make a simple atm program but after login my program error thx for ur help and sorry for my bad english
Code: Pascal  [Select][+][-]
  1. {File   : PenarikanTunai.pas}
  2. {Desc   : Penarikan Tunai:login,tarik tunai,report penarikan}
  3. program penarikanTunai;
  4. uses crt;
  5. type
  6.         nasabah=record
  7.         norek,pin,saldo:longint;
  8.         end;
  9. var
  10.         n:nasabah;
  11.         a:array [1..5] of nasabah;
  12.         b:array [1..100] of integer;
  13.         d,i,x,s,pilihan,p:integer;
  14.         tmp:nasabah;
  15.         found:boolean;
  16.  
  17.  
  18. procedure data(var a:array of nasabah);
  19. begin
  20.         a[1].norek:=0001;
  21.         a[1].pin:=1231;
  22.         a[1].saldo:=1000000;
  23.         a[2].norek:=0002;
  24.         a[2].pin:=1232;
  25.         a[2].saldo:=2000000;
  26.         a[3].norek:=0003;
  27.         a[3].pin:=1233;
  28.         a[3].saldo:=3000000;
  29.         a[4].norek:=0004;
  30.         a[4].pin:=1234;
  31.         a[4].saldo:=4000000;
  32. end;
  33.  
  34. procedure login(var a:array of nasabah;x:integer;found:boolean);
  35. var
  36.         i:integer;
  37. begin
  38.         i:=1;
  39.         found:=false;
  40.         while ((i<5) and not(found)) do
  41.         begin
  42.                 if a[i].pin=x then
  43.                         found:=true
  44.                 else
  45.                         i:=i+1;
  46.         end;
  47.  
  48. end;
  49.  
  50. {function saldo(s:nasabah;p:integer):integer;
  51. begin
  52.         saldo:=s.saldo-p;
  53. end;}
  54.  
  55. procedure jumlahpenarikan(var a:array of integer);
  56. var
  57.         i,j,tmp:integer;
  58. begin
  59.         for i:=2 to 5 do
  60.         begin
  61.                 tmp:=a[i];
  62.                 j:=i;
  63.                 while ((j>1) and (tmp<a[j-1])) do
  64.                 begin
  65.                         a[j]:=a[j-1];
  66.                         j:=j-1;
  67.                 end;
  68.                 a[j]:=tmp
  69.         end;
  70. end;
  71.  
  72. begin
  73.         clrscr;
  74.         d:=0;
  75.         writeln('Masukan Pin');readln(x);
  76.         data(a);
  77.         login(a,x,found);
  78.         {writeln(a[i].saldo);
  79.         readln;}
  80.         {if found=false then
  81.         begin
  82.                 for i:=1 to 2 do
  83.                 begin
  84.                         writeln('===Masukan Pin===');readln(x);
  85.                         data(a);
  86.                         login(a,x,found);
  87.                         tmp:=a[i]
  88.                 end;
  89.         end;}
  90.  
  91.         writeln('===Menu===');
  92.         writeln('1.Tarik Tunai');
  93.         writeln('2.Report Penarikan');
  94.         write('Pilih;');
  95.         readln(pilihan);
  96.  
  97.         case pilihan of
  98.         1: begin
  99.                 writeln('Masukan Jumlah Penarikan:');readln(p);
  100.                 if tmp.saldo>=p then
  101.                 begin
  102.                         if (p>2500000) then
  103.                                 writeln('Penarikan Maksimal adalah 2500000');
  104.                         if(tmp.saldo>=p) then
  105.                                 d:=d+1;
  106.                                 clrscr;
  107.                                 gotoxy(25,10);writeln('Tarik Tunai Berhasil');
  108.                                // writeln('Saldo Anda:',saldo(tmp.saldo,p));
  109.                                 readln;
  110.                                        b[d]:=tmp.saldo-p;
  111.                 end
  112.                 else
  113.                         clrscr;
  114.                         gotoxy(25,10);writeln('Error');
  115.                         readln;
  116.              end;
  117.         2: begin
  118.                 clrscr;
  119.                 jumlahpenarikan(b);
  120.                 for s:=1 to d do
  121.                 begin
  122.                         writeln(b[d]);
  123.                 end;
  124.                 readln;
  125.            end;
  126. end;
  127. end.
  128.  
« Last Edit: April 25, 2016, 03:44:09 pm by frozensiswa »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12850
  • FPC developer.
Re: Help Me
« Reply #1 on: April 25, 2016, 04:07:42 pm »
Sounds like an array out of bounds access, enable all range and overflow checks.

Add {$R+}{$Q+} after the "program" statement.

Bart

  • Hero Member
  • *****
  • Posts: 5717
    • Bart en Mariska's Webstek
Re: Help Me
« Reply #2 on: April 25, 2016, 04:13:49 pm »
I don't uderstand the language, so I don't exactly know what it is supposed to do but:

Here are the compiler messages (I saved the project as atm.lpr):

atm.lpr(76,15) Hint: Variable "a" does not seem to be initialized
atm.lpr(77,24) Warning: Variable "found" does not seem to be initialized
atm.lpr(100,20) Warning: Variable "tmp" does not seem to be initialized
atm.lpr(10,9) Note: Local variable "n" not used
atm.lpr(13,11) Note: Local variable "i" not used

In line 100 you do:
Code: [Select]
if tmp.saldo>=p then

This is likely to give you an access violation right from te start.
Fix the compiler warnigs first.

Code: [Select]
procedure login(var a:array of nasabah;x:integer;found:boolean);

I suppose the found parameter is there to indicate wether or not  something is found (a pin)?
If so then either declare found as an out parameter (or var, if you must be TurboPascal compatible), or more logical to me: declare it as a function:

Code: [Select]
procedure login(var a:array of nasabah;x:integer): Boolean;

Line 104:
Code: [Select]
if(tmp.saldo>=p) then

This is redundant, you are already in a block that is only executed if (tmp.saldo>=p) (line 100)

Please give your variable names that make sense.
It's perfectly OK to have i, j etc as loop variables but almost anything else would benefit from a more descriptive name.

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5717
    • Bart en Mariska's Webstek
Re: Help Me
« Reply #3 on: April 25, 2016, 04:17:49 pm »
Sounds like an array out of bounds access, enable all range and overflow checks.
Add {$R+}{$Q+} after the "program" statement.

It does not crash here (compiled with all checks enabled), but you're probably right: tmp is not initialized.

B.t.w. the RTE: 4209766, isn't that a bit strange? I was under the impression RTE numbers were byte or word?

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5717
    • Bart en Mariska's Webstek
Re: Help Me
« Reply #4 on: April 25, 2016, 04:49:07 pm »
I don't uderstand the language, so I don't exactly know what it is supposed to do:

Google Traslate:
Penarikan=Withdrawal 
tunai=cash
norek = mink, but probably account number?
nasabah=client       
jumlah penarikan = the number of withdrawals
Masukan Pin= Feedback Pin (probably: Enter Pin)
Tarik Tunai = Cash withdrawal (Pull cash)
Report Penarikan = Report Withdrawal
Masukan Jumlah Penarikan=Enter the Total Withdrawal
Penarikan Maksimal adalah 2500000=Maximum withdrawal is 2500000
Tarik Tunai Berhasil=Pull Cash Successfully
Saldo Anda=your balance (typo: must be salod?? according to Google Translate)
Pilih=Choose

Bart
« Last Edit: April 25, 2016, 05:28:57 pm by Bart »

Bart

  • Hero Member
  • *****
  • Posts: 5717
    • Bart en Mariska's Webstek
Re: Help Me
« Reply #5 on: April 25, 2016, 05:42:55 pm »
I cleaned up a little bit, so the code is easier to read for most of us.

Code: [Select]
{File   : PenarikanTunai.pas}
{Desc   : Penarikan Tunai:login,tarik tunai,report penarikan}     //Penarikan=Withdrawal  tunai=cash
program atm;
uses crt;
type
        TClientRec=record
          norek,pin,saldo:longint;        //norek = mink, but probably account number?
        end;
        TClientArray = Array[1..5] of TClientRec;
        TIntegerArray = Array[1..100] of Integer;
var
        //n:TClientRec;        //client
        Clients: TClientArray; //array [1..5] of TClientRec;
        b:TIntegerArray;//array [1..100] of integer;
        d,{i,}Pin,s,Choice,Amount:integer;  //pilihan = selection
        tmp:TClientRec;
        //found:boolean;


procedure data(out TheClients: TClientArray{array of TClientRec});
begin
        TheClients[1].norek:=0001;
        TheClients[1].pin:=1231;
        TheClients[1].saldo:=1000000;
        TheClients[2].norek:=0002;
        TheClients[2].pin:=1232;
        TheClients[2].saldo:=2000000;
        TheClients[3].norek:=0003;
        TheClients[3].pin:=1233;
        TheClients[3].saldo:=3000000;
        TheClients[4].norek:=0004;
        TheClients[4].pin:=1234;
        TheClients[4].saldo:=4000000;
end;

function login(var Clients: TClientArray{array of TClientRec}; x:integer; out AClient: TClientRec): Boolean;
var
        i:integer;
begin
        i:=1;
        Result:=false;
        while ((i<5) and not(Result)) do
        begin
                if Clients[i].pin=x then
                begin
                        Result:=true;
                        AClient:=Clients[i]
                end
                else
                        i:=i+1;
        end;

end;

function saldo(AClient:TClientRec; AValue:integer):integer;
begin
        saldo:=AClient.saldo-AValue;
end;

//I cannot figure out what it is supposed to do
procedure jumlahpenarikan(var a:TIntegerArray{array of integer}); //jumlah penarikan = the number of withdrawals
var
        i,j,tmp:integer;
begin
        for i:=2 to 5 do
        begin
                tmp:=a[i];
                j:=i;
                while ((j>1) and (tmp<a[j-1])) do
                begin
                        a[j]:=a[j-1];
                        j:=j-1;
                end;
                a[j]:=tmp
        end;
end;

begin
        clrscr;
        d:=0;
        writeln('Masukan Pin (Enter Pin)');readln(Pin);   //Masukan = Feedback
        data(Clients);
        if not login(Clients,Pin,tmp) then tmp := Clients[1]; // just for now assume succefull login
        {writeln(Clients[i].saldo);
        readln;}
        {if found=false then
        begin
                for i:=1 to 2 do
                begin
                        writeln('===Masukan Pin===');readln(Pin);
                        data(Clients);
                        login(Clients,Pin,found);
                        tmp:=Clients[i]
                end;
        end;}

        writeln('===Menu===');
        writeln('1.Tarik Tunai (Cash Withdrawal)');   //Tarik Tunai = Cash withdrawal (Pull cash)
        writeln('2.Report Penarikan (Report WithDrawal)');//Report Penarikan = Report Withdrawal
        write('Pilih (Choose): ');
        readln(Choice);

        case Choice of
        1: begin
                write('Masukan Jumlah Penarikan (Enter total withdrawal): ');
                readln(Amount);     //Masukan Jumlah Penarikan=Enter the Total Withdrawal
                if tmp.saldo>=Amount then
                begin
                        if (Amount>2500000) then
                        begin
                          clrscr;
                          gotoxy(15,10);
                          writeln('Penarikan Maksimal adalah 2500000 (Maximum withdrawal is 2500000)');  //Penarikan Maksimal adalah 2500000=Maximum withdrawal is 2500000
                        end
                        else
                        begin
                        //if(tmp.saldo>=Amount) then  //redundant we already know this is true
                                d:=d+1;
                                clrscr;
                                gotoxy(15,10);
                                writeln('Tarik Tunai Berhasil (Pull Cash Successfully)');  //Tarik Tunai Berhasil=Pull Cash Successfully
                                gotoxy(15,11);
                                writeln('Saldo Anda (your balance): ',saldo(tmp,Amount));  //Saldo Anda=your balance (typo: must be salod??)
                                readln;
                                b[d]:=tmp.saldo-Amount;
                        end;
                end
                else
                begin
                        clrscr;
                        gotoxy(15,10);writeln('Error: insuffcient saldo: ',tmp.saldo,', requested amount was: ',Amount);
                        readln;
                end;
             end;
        2: begin
                clrscr;
                jumlahpenarikan(b);
                for s:=1 to d do    //note: at this point d always equals 0, so nothing happens
                begin
                        writeln(b[d]);
                end;
                readln;
           end;
end;

end.

Notes:
  • I gave all variables and types a descriptive name as far as I understand the code
  • I added type definition for the array of clients, it is needed for Data procedure, it won't function as intended in the way it was written before
  • The login routine now retruns True upon succes, and in that case also returns the found client
  • Uncommented the saldo procedure
  • If requested amount > 2500000 only don't bother checking if it's > current saldo
  • Added some clrscr's and gotoxy()'s

Several issues remain:

  • You must handle a failed login
  • You can only make 1 withdrawal and the program ends
  • It's a mystery to me what the "jumlahpenarikan" procdure is supposed to do
  • If you choose "2", nothing happens, see the comments in the code.
  • The progrm crashes if user input is invalid (e.g. user enters 'abc' where program expects a number

Hope this helps you a little bit.

Bart

Handoko

  • Hero Member
  • *****
  • Posts: 5537
  • My goal: build my own game engine using Lazarus
Re: Help Me
« Reply #6 on: April 25, 2016, 06:09:35 pm »
Let me help to translate.

penarikan = withdrawal
tunai = cash
nasabah = client
jumlah penarikan = amount of withdrawal
penarikantunai = cash withdrawal
no_rek = account_number
pin = password
saldo = balance
masukan pin = enter your password
masukan jumlah penarikan = enter the amount of withdrawal
penarikan maksimal adalah = maximum withdrawal amount is
tarik tunai berhasil = cash withdrawal successfully
saldo anda = your balance
pilih = choose

Bart

  • Hero Member
  • *****
  • Posts: 5717
    • Bart en Mariska's Webstek
Re: Help Me
« Reply #7 on: April 25, 2016, 06:32:56 pm »
Let me help to translate.

Got most of that (see my attempt above).

Bart

 

TinyPortal © 2005-2018