Board index » delphi » need help on prcedures (please help)

need help on prcedures (please help)

Ok, here's the output I want:

This program is to do your calculating.
Please enter A for Adding
S for subtracting
M for multiply

{User enters A}

Enter a number:
{User enters x}
Enter another number
{User enters x}
The answer is n.

And so forth.

This how i did mine and it was kinda unsuccessful.

Program Calculating;
var c:char;
Begin
  writeln('This program is to do your calculating');
  writeln('Please enter A for Adding');
  writeln('S for sub')
  writeln('M for multipyling')
{ now here's where things get messed up}
IF c:=A then goto Adding;
IF c:=S then goto Subtracting;
{and so on}

Procedure Adding;
 var bla bla bla
{ I know how to do this part}
It's calling up the procedure that is hard for me

i wonder if anybody can correct me or better still give me a sample :)

THANKS!!!!

a...@tm.net.my

 

Re:need help on prcedures (please help)


In article <01bdc314$283eabc0$b211bcca@nigellee>, a...@tm.net.my says...
Quote
> Ok, here's the output I want:

> This program is to do your calculating.
> Please enter A for Adding
> S for subtracting
> M for multiply

> {User enters A}

> Enter a number:
> {User enters x}
> Enter another number
> {User enters x}
> The answer is n.

> And so forth.

> This how i did mine and it was kinda unsuccessful.

> Program Calculating;
> var c:char;
> Begin
>   writeln('This program is to do your calculating');
>   writeln('Please enter A for Adding');
>   writeln('S for sub')
>   writeln('M for multipyling')
> { now here's where things get messed up}
> IF c:=A then goto Adding;
> IF c:=S then goto Subtracting;
> {and so on}

> Procedure Adding;
>  var bla bla bla
> { I know how to do this part}
> It's calling up the procedure that is hard for me

> i wonder if anybody can correct me or better still give me a sample :)

> THANKS!!!!

> a...@tm.net.my

I will give you 2 version, but I personally thought that if there is only
one answer use a function will look cleaner(well I could be wrong, you
know). I am not against the statement 'goto' but try not to use it unless
absoutely (which is very seldom) necessary. Any more question feel free
to ask me at yaot...@mbox2.singnet.com.sg. I will try to help you as much
as I can (but don't abuse it).

Program Calculating;
{Function Edition}

Function Adding(Input1 , Input2 : double) : double;

Begin

        Adding := Input1 + Input2;
End;

Function Subtract(Input1, Input2 : double) : double;

Begin

        Subtract := Input1 - Input2;
End;

Function Multiply(Input1, Input2 : double) : double;

Begin
        Multiply := Input1 * Input2 : double) : double;

End;

var c            :    char;
    num1, num2   :    double;
    answer          :    double;

Begin
  writeln('This program is to do your calculating');
  writeln('Please enter A for Adding');
  writeln('S for sub');
  writeln('M for multipyling');
  writeln
  write('Enter your choice : ');
  readln(c);  
  writeln('____________________________________________________');

  write('Enter two number for ');
  case  c of
  A             :       Begin
                         write('adding : ');
                       readln(num1,num2);
                       output := Adding(num1,num2);
                      end

  S             :       Begin
                         write('subtracting (num1 - num2): ');
                       readln(num1,num2);
                       output := subtract(num1,num2);
                      end

  M             :       Begin
                         write('Multipying : ');
                       readln(num1,num2);
                       output := Multiply(num1,num2);
                      end

End;

writeln;
writeln;
writeln('The output is ',output:2:2);

End.

___________________________________________________________

Program Calculating;
{Procedure Edition}

Procedure Adding(Input1 , Input2 : double; VAR Output : double);

Begin

        Adding := Input1 + Input2;
End;

Function Subtract(Input1, Input2 : double; VAR Output : double);

Begin

        Subtract := Input1 - Input2;
End;

Function Multiply(Input1, Input2 : double; VAR Output : Double);

Begin
        Multiply := Input1 * Input2 : double) : double;

End;

var c            :    char;
    num1, num2   :    double;
    answer          :    double;

Begin
  writeln('This program is to do your calculating');
  writeln('Please enter A for Adding');
  writeln('S for sub');
  writeln('M for multipyling');
  writeln
  write('Enter your choice : ');
  readln(c);  
  writeln('____________________________________________________');

  write('Enter two number for ');
  case  c of
  A             :       Begin
                         write('adding : ');
                       readln(num1,num2);
                       Adding(num1,num2,output);
                      end

  S             :       Begin
                         write('subtracting (num1 - num2): ');
                       readln(num1,num2);
                       Subtract(num1,num2,output);
                      end

  M             :       Begin
                         write('Multipying : ');
                       readln(num1,num2);
                       Multiply(num1,num2,output);
                      end

End;

writeln;
writeln;
writeln('The output is ',output:2:2);

End.

Re:need help on prcedures (please help)


Quote
nigel wrote in message <01bdc314$283eabc0$b211bcca@nigellee>...
>Ok, here's the output I want:

>This program is to do your calculating.
>Please enter A for Adding
>S for subtracting
>M for multiply

>{User enters A}

>Enter a number:
>{User enters x}
>Enter another number
>{User enters x}
>The answer is n.

69696969696969696969696969696969696969696969696969
Well, hope this will work for you....

Program Calculate;

var
  choose:char;
  num1,num2: integer;

Procedure Adding(num1,num2:integer);
begin
  WriteLn(num1, ' + ', num2,' = ',num1+num2);
end;

Procedure Subtracting(num1,num2:integer);
begin
  WriteLn(num1, ' - ', num2,' = ',num1-num2);
end;

Procedure Multiplying(num1,num2:integer);
begin
  WriteLn(num1, ' * ', num2,' = ',num1*num2);
end;

Begin
  WriteLn('A = Adding');
  WriteLn('S = Subtracting');
  WriteLn('M = Multiplying');
  Write('Please enter the letter of your choice: ');
  ReadLn(choose);
  Write('Enter first number: ');
  ReadLn(num1);
  Write('Enter second number: ');
  ReadLn(num2);
  If choose = 'A' then Adding(num1,num2)
   Else If choose = 'S' then Subtracting(num1,num2)
     Else If choose = 'M' then Multiplying(num1,num2)
        Else WriteLn('Error - Invalid choice!');
End.

The above code is just a sample.  The code below is how I'd do it.

Program Calculate;

var
  choice: char;
  num1,num2:integer;

Begin
  WriteLn('[A]dding / [S]ubtracting / [M]ultiplying');
  WriteLn('What would you like to do? [A/S/M]:');
  choice := readkey;
  Write('Enter first number: ');
  ReadLn(num1);
  Write('Enter second number: ');
  ReadLn(num2);
  Case Upcase(choice) of
    'A': WriteLn(num1, ' + ', num2,' = ',num1+num2);
    'S': WriteLn(num1, ' - ', num2,' = ',num1-num2);
    'M': WriteLn(num1, ' * ', num2,' = ',num1*num2);
    else
        WriteLn('Error - Invalid choice!);
   end;
End.
696969696969696969696969696969696969696969696969

Other Threads