Computer Science Canada

calculator error

Author:  con.focus [ Thu Dec 02, 2004 6:15 pm ]
Post subject:  calculator error

im making a calculator simple though every time when usinmg the divison function and the secound number is 0 is outputs an error ne way i can beat the programm to it by tell is to exit the function and output an error message if the secound number is 0 and the function being used is division
here's my code
code:
var inum1,inum2 : real
var ch :char
get inum1
get inum2
get ch
if ch = '/' then
put inum1 / inum2
elsif ch = '*' then
put inum1 * inum2
elsif ch = '-' then
put inum1 - inum2
elsif ch = '+' then
put inum1 + inum2
end if

Author:  Mazer [ Thu Dec 02, 2004 6:21 pm ]
Post subject: 

Just check to make sure that inum2 isn't equal to 0. If it's zero and they choose '/' then just don't do the operation. Try this in your if statement:

code:
if ch = '/' then
if inum2 not= 0 then
put inum1 / inum2
else
put "Can't divide by 0"
end if
%rest of if statement here

Author:  con.focus [ Thu Dec 02, 2004 6:36 pm ]
Post subject: 

thx alot man i had to change a bit but the right idea i needed


: