954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

basic help needed with pascal application

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.
StanRogo
Newbie Poster
3 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

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

try
  num1 := StrToFloat(edNum1.Text);
except
  num1 := 0;
end;
pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

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?

StanRogo
Newbie Poster
3 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

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


.....

NotNil
Newbie Poster
8 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 

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.

StanRogo
Newbie Poster
3 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: