Help vhit super simple program

Please use this Board for QBasic related requests ( file research, programming, etc.)

Moderators: Administrator, Global Moderator

Post Reply
LuxAngelus

Help vhit super simple program

Post 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?
Dr_Davenstein
QBasic God
Posts: 166
Joined: Tue Mar 25, 2003 12:45 am
Location: U.S.A.
Contact:

Post 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
Guest

Post 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]
Post Reply