Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi: Incompatible types: 'integer' and 'extended'

Tags:

delphi

I need to make a program that works out the amount you will get paid for the hours you worked. Here's the code:

unit HoursWorked_u;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Spin;

type
  TForm1 = class(TForm)
    lblName: TLabel;
    edtName: TEdit;
    Label1: TLabel;
    sedHours: TSpinEdit;
    btncalc: TButton;
    Panel1: TPanel;
    lblOutput: TLabel;
    Label2: TLabel;
    Panel2: TPanel;
    lblOutPutMonth: TLabel;
    labelrandom: TLabel;
    Label3: TLabel;
    seddays: TSpinEdit;
    procedure btncalcClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
// sedHours and sedDays are SpinEdits
// Rand (R) is South African currency eg: One months work, I will recieve-
// -R10 000.00
// Where R12,50 is paid an hour.
// Need to work out how much I will get paid for how  many hours are worked.
procedure TForm1.btncalcClick(Sender: TObject);
var
    sName                       :string;
    iHours, iDays               :integer;
    rPay                        :real;

begin
  rPay := 12.5;
  sName := edtName.Text;
  iHours := sedHours.value * rPay;
  iDays := sedDays.value * iHours;
    lblOutput.caption := sName + ' You will recieve R' + IntToStr (iHours);
    lblOutputMonth.Caption := 'You will recive R' + intToStr (iDays);
end;

end.

The error msg is:

[Error] HoursWorked_u.pas(51): Incompatible types: 'Integer' and 'Extended'

Please do note: I am a novice user at coding all together and this is IT homework. Any help would be much appreciated! Thanks in advance!

like image 772
Aidan Quinn Avatar asked Feb 05 '14 21:02

Aidan Quinn


1 Answers

The error is here:

iHours := sedHours.value * rPay;

The right hand side is a floating point expression because rPay is a floating point variable. You cannot assign a floating point value to an integer. You need to convert to an integer.

For instance, you might round to the nearest:

iHours := Round(sedHours.value * rPay);

Or you might use Floor to get the largest integer less than or equal to the floating point value:

iHours := Floor(sedHours.value * rPay);

Or perhaps Ceil, the smallest integer greater than or equal to the floating point value:

iHours := Ceil(sedHours.value * rPay);

For some more general advice I suggest that you try to look in the documentation when you encounter an error that you do not understand. Every compiler error is documented. Here's the documentation for E2010 Incompatible types: http://docwiki.embarcadero.com/RADStudio/en/E2010_Incompatible_types_-_%27%25s%27_and_%27%25s%27_%28Delphi%29

Take a good read of it. Although the example given is not an exact match for your case it is very close. Compiler errors are not things to be scared of. They come with descriptive text and you can solve your problem by reading them and trying to work out how your code has led to the particular error.

like image 68
David Heffernan Avatar answered Oct 27 '22 20:10

David Heffernan