Now I have been doing pascal for three months. I am now trying to create a simple calculator program. However, the TEdit box is not working. I type in the value but it seems it is not recording since when converted to either a float or int it states that its value is "" meaning it hasn't taken in the data that was entered. How can i get around this. my code is below:

unit calc;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls;

type

  { TCalculator }

  TCalculator = class(TForm)
    CbOperation: TComboBox;
    edNum1: TEdit;
    edNum2: TEdit;
    lblTotal: TLabel;
    lblNumber1: TLabel;
    lblNumber2: TLabel;
    procedure CbOperationChange(Sender: TObject);
    procedure CbOperationSelect(Sender: TObject);
    procedure edNum1Change(Sender: TObject);
    procedure edNum2Change(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end; 

var
  Calculator: TCalculator;
  total,num1,num2:real;

implementation

{ TCalculator }

procedure TCalculator.CbOperationChange(Sender: TObject);
begin

end;

procedure TCalculator.CbOperationSelect(Sender: TObject);
begin



    if cbOperation.items.text = 'add' then
  total:=num1+num2;
  if cbOperation.items.text = 'subtract' then
  total:=num1-num2;
  if cbOperation.items.text = 'multiply' then
  total:=num1*num2;

    lblTotal.caption:=floattostr(total);
end;

procedure TCalculator.edNum1Change(Sender: TObject);
begin
     num1:=strtofloat(edNum1.text);
end;

procedure TCalculator.edNum2Change(Sender: TObject);
begin
     num2:=strtofloat(edNum2.text);
end;


initialization
  {$I calc.lrs}

  num1:=0;
  num2:=0;
  total:=0;
end.

StrToFloat raises an exception when the value is empty, try this:

try
  num1 := StrToFloat(edNum1.Text);
except
  num1 := 0;
end;

StrToFloat raises an exception when the value is empty, try this:

try
  num1 := StrToFloat(edNum1.Text);
except
  num1 := 0;
end;

Well that fixed the error messages but the answer is still 0 because the text I type in the application is somehow not picked up by the machine and still = 0 or "" even when a number like 225 is typed. Help?

Ok I see a small issue with the following code:

if cbOperation.items.text = 'add' then

total:=num1+num2;

if cbOperation.items.text = 'subtract' then

total:=num1-num2;

if cbOperation.items.text = 'multiply' then

total:=num1*num2;

it should read as follows:

if cbOperation.text = 'add' then

total:=num1+num2;

if cbOperation.text = 'subtract' then

total:=num1-num2;

if cbOperation.text = 'multiply' then

total:=num1*num2;

the difference is that I have removed the

.items

portion of your code. You dont need it. It wont work

once you have fixed that... another thing you may want to check is that the value you are comparing... for example 'add'... actually does match the value displayed in cbOperation.text

or even do a conversion to uppercase to ensure text values are compariable...

foe example:

if UpperCase(cbOperation.text) = 'ADD' then

.....

Thanks a bunch. Sorry that it was such a simple error but I am very knew to this. 2 months doing AS computing at the moment.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.