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?
Help vhit super simple program
Moderators:Administrator, Global Moderator
-
- QBasic God
- Posts:166
- Joined:Tue Mar 25, 2003 12:45 am
- Location:U.S.A.
- Contact:
Well, you can add an error checking "IF...THEN" statement before the division line.
Something like this....
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
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:
Does the above help you?
Ralph A.[/code]
"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:
Ralph A.[/code]