Page 1 of 1

Help vhit super simple program

Posted: Sun Oct 10, 2004 9:21 pm
by LuxAngelus
I am a total n00b whit QB and im trying to make a simple canculator
program. It looks like this:
CLS
INPUT "First number ", A
INPUT "Second number ", B
PRINT "A + B = " A + B
PRINT "A - B = " A - B
PRINT "A * B = "A * B
PRINT "A / B = "A / B

It works fine until I imput 0 as the Second number.
The error Division by zero
What can i do to stop this from hapening?

Posted: Sun Oct 10, 2004 9:39 pm
by Dr_Davenstein
Well, you can add an error checking "IF...THEN" statement before the division line.

Something like this....

Code: Select all

CLS
INPUT "First number ", A
INPUT "Second number ", B
PRINT "A + B = " A + B
PRINT "A - B = " A - B
PRINT "A * B = "A * B
If B=0 then B=1
PRINT "A / B = "A / B

Posted: Fri Nov 05, 2004 4:52 pm
by Guest
For a value of B = 0, I prefer to not give an answer of:
"A / B = "A/1
when that is NOT the correct answer.
So, for the problem with division by zero, I would change your code to:

Code: Select all

CLS 
INPUT "First number ", A 
INPUT "Second number ", B 
PRINT "A + B = " A + B 
PRINT "A - B = " A - B 
PRINT "A * B = "A * B 

IF B = 0 THEN
  PRINT " DIVIDION BY ZERO IS A MATHEMATICAL NO-NO, SO NO ANSWER CAN BE GIVEN TO A/0"
  GOTO NoZero:
END IF

PRINT "A / B = "A / B 
NoZero:
Does the above help you?

Ralph A.[/code]