Posts: 33
Threads: 8
Joined: Jan 2006
Hey guys I wrote up my project but it doesnt seem to work. I think I got a problem somewhere but cant spot it
Help would be appreciated.
Code:
CLS
start:
PRINT "Celsius -- Fahrenheit / Fahrenheit -- Celsius Converter"
PRINT
PRINT
PRINT "1)Celsius -- Fahrenheit"
PRINT
PRINT "2)Fahrenheit -- Celsius"
PRINT
INPUT "Enter your Choice:", selection%
IF selection% = 1 THEN
CLS
PRINT "Celsius -- Fahrenheit Conversion."
PRINT
INPUT "Enter Temperature in Celsius:"; temp%
PRINT
fahrenheit! = temp% * 9 / 5 + 32
PRINT "Fahrenheit:", fahrenheit!
IF selection% = 2 THEN
CLS
PRINT "Fahrenheit -- Celsius Conversion"
PRINT
INPUT "Enter Temperature in Fahrenheit:"; temp1%
celsius! = (temp1% - 32) * 5 / 9
PRINT
PRINT "Celsius:", celsius!
ELSE PRINT "Invalid Selection" GOTO START
END IF
img]http://img213.imageshack.us/img213/6104/sig1jb.gif[/img]
Posts: 199
Threads: 2
Joined: May 2003
If you try to run the program the QBASIC IDE tells you the problems with the code.
- You have 2 commands on one line (QBASIC has highlighted this line as an error):
Code:
ELSE PRINT "Invalid Selection" GOTO START
END IF
A block IF...ELSE...END IF has the ELSE and all the commands on seperate lines. e.g.
Code:
ELSE
PRINT "Invalid Selection"
GOTO START
END IF
- The next error QBASIC shows is "Block IF without END IF". You have 2 block IF statements but only one END IF. There are many ways you could fix this but I would change the line
Code:
IF selection% = 2 THEN
to Code:
ELSEIF selection% = 2 THEN
therefore continuing the IF block.[/list:o]
Your corrected code:Code:
CLS
start:
PRINT "Celsius -- Fahrenheit / Fahrenheit -- Celsius Converter"
PRINT
PRINT
PRINT "1)Celsius -- Fahrenheit"
PRINT
PRINT "2)Fahrenheit -- Celsius"
PRINT
INPUT "Enter your Choice:", selection%
IF selection% = 1 THEN
CLS
PRINT "Celsius -- Fahrenheit Conversion."
PRINT
INPUT "Enter Temperature in Celsius:"; temp%
PRINT
fahrenheit! = temp% * 9 / 5 + 32
PRINT "Fahrenheit:", fahrenheit!
ELSEIF selection% = 2 THEN
CLS
PRINT "Fahrenheit -- Celsius Conversion"
PRINT
INPUT "Enter Temperature in Fahrenheit:"; temp1%
celsius! = (temp1% - 32) * 5 / 9
PRINT
PRINT "Celsius:", celsius!
ELSE
PRINT "Invalid Selection"
GOTO START
END IF
If you want to check multiple conditions against a varable then I feel the SELECT CASE command is more useful.
Your code with SELECT CASE insted of IF...ELSEIF...ELSE..END IF:
Code:
CLS
START:
PRINT "Celsius -- Fahrenheit / Fahrenheit -- Celsius Converter"
PRINT
PRINT
PRINT "1)Celsius -- Fahrenheit"
PRINT
PRINT "2)Fahrenheit -- Celsius"
PRINT
INPUT "Enter your Choice:", selection%
SELECT CASE selection%
CASE 1
CLS
PRINT "Celsius -- Fahrenheit Conversion."
PRINT
INPUT "Enter Temperature in Celsius:"; temp%
PRINT
fahrenheit! = temp% * 9 / 5 + 32
PRINT "Fahrenheit:", fahrenheit!
CASE 2
CLS
PRINT "Fahrenheit -- Celsius Conversion"
PRINT
INPUT "Enter Temperature in Fahrenheit:"; temp1%
celsius! = (temp1% - 32) * 5 / 9
PRINT
PRINT "Celsius:", celsius!
CASE ELSE
PRINT "Invalid Selection"
GOTO START
END SELECT
Hope this helps.
url=http://www.spreadfirefox.com/?q=affiliates&id=60131&t=79]
[/url]
END OF LINE
.
Posts: 1,956
Threads: 65
Joined: Jun 2003
Phydaux,
Very nice problem explanation and code fixup.
*****
Posts: 2,771
Threads: 96
Joined: Oct 2003
Woah, Phydaux! Welcome back. Youve been gone about 10 months!
Where did you get to?
Posts: 33
Threads: 8
Joined: Jan 2006
Thanx for the help mate.
img]http://img213.imageshack.us/img213/6104/sig1jb.gif[/img]
Posts: 199
Threads: 2
Joined: May 2003
@
Moneo: Thank You.
@
KiZ: I lost intrest in QB a while ago, quite a few real life problems got in the way of little hobbies like this, but I've just got a new job where they actually pay me to code VB, create Spreadsheets and Databases (the fools
) So I'm getting back into QB.
@
Hybr!d: Glad I could help.
url=http://www.spreadfirefox.com/?q=affiliates&id=60131&t=79]
[/url]
END OF LINE
.