Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Celsius ---->Fahrenhiet and vice versa problem
#1
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]
Reply
#2
If you try to run the program the QBASIC IDE tells you the problems with the code.

  1. 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

  2. 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. Smile
url=http://www.spreadfirefox.com/?q=affiliates&id=60131&t=79][Image: safer.gif][/url]
END OF LINE.
Reply
#3
Phydaux,

Very nice problem explanation and code fixup.
*****
Reply
#4
Woah, Phydaux! Welcome back. Youve been gone about 10 months!

Where did you get to?
Reply
#5
Thanx for the help mate.
img]http://img213.imageshack.us/img213/6104/sig1jb.gif[/img]
Reply
#6
@Moneo: Thank You. Smile

@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 Wink) So I'm getting back into QB. Smile

@Hybr!d: Glad I could help. Smile
url=http://www.spreadfirefox.com/?q=affiliates&id=60131&t=79][Image: safer.gif][/url]
END OF LINE.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)