Hello,

I am trying to write this program in Delphi that calculates the amount of change required to make a certain amount.

The user enter a number between 1 and 200. The program then calculates the amount of 100, 50, 20, 10, 5, 2, 1 bils required.

But there is a problem it doesnt always works.
When I enter the number '123' it says I need 1 - '100', 1 - '20', 1 - '2' and 1 - '1' dolar bills.
but when I enter it says I need 1 - '100', 1 - '2' and 1 - '2' dollar bils. it should say that I need 2 - '2' dollar bils

I think its something wrong with my algorithm where I am not checking the values correctly

Here is my code:

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Samples.Spin,
  Vcl.Buttons;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    sedAmount: TSpinEdit;
    Button1: TButton;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    lblR100: TLabel;
    lblR50: TLabel;
    lblR20: TLabel;
    lblR10: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    lblR5: TLabel;
    lblR2: TLabel;
    lblR1: TLabel;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);

    procedure UpdateLabels();
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  iAmount: integer;
  iLeftOver: integer;

  iR100, iR50, iR20, iR10, iR5, iR2, iR1: integer;

implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  lblR100.Caption := '0';
  lblR50.Caption := '0';
  lblR20.Caption := '0';
  lblR10.Caption := '0';
  lblR5.Caption := '0';
  lblR2.Caption := '0';
  lblR1.Caption := '0';
end;

procedure TForm1.UpdateLabels();
begin
  lblR100.Caption := IntToStr(iR100);
  lblR50.Caption := IntToStr(iR50);
  lblR20.Caption := IntToStr(iR20);
  lblR10.Caption := IntToStr(iR10);
  lblR5.Caption := IntToStr(iR5);
  lblR2.Caption := IntToStr(iR2);
  lblR1.Caption := IntToStr(iR1);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  iR100 := 0;
  iR50 := 0;
  iR20 := 0;
  iR10 := 0;
  iR5 := 0;
  iR2 := 0;
  iR1 := 0;

  iAmount := StrToInt(sedAmount.Text);

  { Now for the complex calculations... }
  if(iAmount >= 100) then
  begin
    iLeftOver := iAmount mod 100;
    iR100 := iR100 + 1;

    if(iLeftOver >= 100) then
    begin
      iLeftOver := iLeftOver mod 100;
      iR100 := iR100 + 1;
    end;

    if(iLeftOver >= 50) then
    begin
      iLeftOver := iLeftOver mod 50;
      iR50 := iR50 + 1;
    end;

    if (iLeftOver >= 20) then
    begin
      iLeftOver := iLeftOver mod 20;
      iR20 := iR20 + 1;
    end;

    if(iLeftOver >= 10) then
    begin
      iLeftOver := iLeftOver mod 10;
      iR10 := iR10 + 1;
    end;

    if(iLeftOver >= 5) then
    begin
      iLeftOver := iLeftOver mod 5;
      iR5 := iR5 + 1;
    end;

    if(iLeftOver >= 2) then
    begin
      iLeftOver := iLeftOver mod 2;
      iR2 := iR2 + 1;
    end;

    if(iLeftOver >= 1) then
    begin
      iLeftOver := iLeftOver mod 1;
      iR1 := iR1 + 1;
    end;

    UpdateLabels();
  end;
  {r100}

end;

end.

Please help me fix this problem.

Edited 2 Years Ago by MasterHacker110

Consider these lines:

      iR100 := iR100 + 1;
      iR50  := iR50 + 1;
      iR20 := iR20 + 1;
      iR10 := iR10 + 1;
      iR5 := iR5 + 1;
      iR2 := iR2 + 1;
      iR1 := iR1 + 1;

You can only ever add 1 to the number of notes of any denomination that you require. Instead of adding 1, add the number of notes you require.
Hint 1: You will find it easier to do this before you change iLeftOver rather than after.
Hint 2: Look up the Div function.

Im sorry I am not sure what you meen I should do. I dont use the div function because the when I mod my number there will be a number left over that will then be mod by the next number.

Suppose iLeftover = 4.
Obviously you need 2 x $2 notes, and no other notes. How can you calculate that?
iLeftover div 2 = 2.
iLeftOver mod 2 = 0.
Do you see now?

Im sorry Im still not sure how I should change my code so that it will work.

Do you meen:

if(iAmount >= 100) then
  begin
    iLeftOver := iAmount div 100;
    iR100 := iLeftOver;

    UpdateLabels();
end;

with that code ill pretty much have to write an if statement for everynumber between 1 and 200.

Edited 2 Years Ago by MasterHacker110

You need an IF for every note value, like you have at the moment, You just need to slightly change the code within each IF. For example, instead of:

    if(iLeftOver >= 2) then
    begin
      iLeftOver := iLeftOver mod 2;
      iR2 := iR2 + 1;
    end;

you could have:

    if(iLeftOver >= 2) then
    begin
      iR2 := iLeftover div 2;
      iLeftOver := iLeftOver mod 2;
    end;
This question has already been answered. Start a new discussion instead.