Fórum Ajuda com programa simples #593154

15/05/2018

0

Bom dia,
Não estou conseguindo executar um programa que calcula a média de dois números (Sou iniciante).

Lá vai:

unit Media;

interface

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

type
TForm1 = class(TForm)
Nota1: TLabel;
Nota2: TLabel;
Media: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
Var
Nota1, Nota2: Integer;
Media: Real;

begin
Nota1:= StrToInt (Edit1.Text);
Nota2:= StrToInt (Edit2.Text);
Media:= (Nota1 + Nota2) / 2;
Media:= RealToStr (Media);

end;


end.

O erro:
[dcc32 Error] Media.pas(41): E2003 Undeclared identifier: 'RealToStr'

Se alguém puder reescrever, agradeceria muito.

Abraço.
Adriano Freitas

Adriano Freitas

Responder

Post mais votado

15/05/2018

Usa o FloatToStr ao invés de RealToStr, e vê se funciona?

Iure Caldas

Iure Caldas
Responder

Gostei + 1

Mais Posts

15/05/2018

Iure Caldas

Usa o FloatToStr ao invés de RealToStr, e vê se funciona?


testei aqui, é isso mesmo, veja:

procedure TForm1.Button1Click(Sender: TObject);
var
n1,n2: Integer;
media: Real;
begin
n1:= StrToInt(Edit1.Text);
n2:= StrToInt(Edit2.Text);
media:= (n1 + n2) / 2;
Edit3.Text:= FloatToStr(media);
end;
Responder

Gostei + 0

15/05/2018

Adriano Freitas

Opa. Tentei assim e não deu certo:

unit Media;

interface

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

type
  TForm1 = class(TForm)
    Nota1: TLabel;
    Nota2: TLabel;
    Media: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
Var
    Nota1, Nota2: Integer;
    Media: Float;

begin
       Nota1:= StrToInt (Edit1.Text);
       Nota2:= StrToInt (Edit2.Text);
       Media:= (Nota1 + Nota2) / 2;
       Media:= FloatToStr (Media);

end;


end.


Erro:


[dcc32 Error] Media.pas(35): E2003 Undeclared identifier: 'Float'
É delphi starter.

Valew
Responder

Gostei + 1

15/05/2018

Natanael Ferreira

Troque o nome do seu Label "Media" (exemplo, troque para lblMedia) porque está conflitando com o nome da sua variável "Media" do tipo real.

Exemplo:

Var
  Nota1, Nota2: Integer;
  Media: Real;
begin
  Nota1 := StrToInt(Edit1.Text);
  Nota2 := StrToInt(Edit2.Text);
  Media := (Nota1 + Nota2) / 2;
  lblMedia.Caption := FloatToStr(Media); // Troquei o nome do label para lblMedia
end;
Responder

Gostei + 1

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar