none
Small Basic with "if" statements and user input?? I'm stumped RRS feed

  • Question

  • So recently I started following the Microsoft Small Basic tutorial, and came across the part where it instructs you to make a Fahrenheit to Celsius converter. 

    I was wondering if I could make a program that combines a Celsius to Fahrenheit converter into the program, and have it let the user choose which converter to use at the beginning? Right now all it does when I try to enter "Celsius" or "Fahrenheit" is that is repeats the question and doesn't proceed. ='(

    I suspect that the

    ch=TextWindow.Read() 

    If "Celsius"

    isn't written correctly for what i'm trying to do...

    This is my code right now, but i'm incredibly new to this so I barely have an idea as how to make this work. =S

    begin:
    TextWindow.Write("Want Celsius or Fahrenheit? ")
    ch = TextWindow.Read()
    If "Celsius" Then
      Goto Fahren
    If "Fahrenheit" Then
      Goto celsius
    EndIf
    celsius:
    TextWindow.Write("Enter Celsius: ")
    c = TextWindow.ReadNumber()
    Fahr = (c*9)/5 +32
    TextWindow.WriteLine (Fahr)
    Fahren:
    TextWindow.Write("Enter Fahrenheit" )
    f = TextWindow.ReadNumber()
    celsius = (5/9)*(f-32)
    TextWindow.WriteLine(celsius)
    Goto begin
    Visual problem



    Sunday, June 24, 2012 2:08 AM

Answers

  • Hi David Darrell

    I've made some tweaks to your code.

    Instead of asking a user to type such long words, why not just request a single key press? We're lazy!

    So, instead of TextWindow.Read(), use TextWindow.ReadKey()!

    Another nicey is to use subroutines for each specialized code you need.

    That is, a sub for Celsius and another one for Fahrenheit.

    See the code below:

    Ask:
    TextWindow.Clear()
    
    TextWindow.WriteLine("Press 'C' for Celsius or 'F' for Fahrenheit")
    ch = Text.ConvertToUpperCase( TextWindow.ReadKey() )
    
    If     ch = "C" Then
      Celsius()
    
    ElseIf ch = "F" Then
      Fahren()
      
    ElseIf ch = "Q" Then
      Program.End()
      
    Else
      TextWindow.WriteLine("Wrong option!!!")
      Sound.PlayBellRing()
      
    EndIf
    
    TextWindow.Pause()
    Goto Ask
    
    Sub Celsius
      TextWindow.Write("Enter Celsius: ")
      Celsius = TextWindow.ReadNumber()
      Fahr = Celsius * 9/5 + 32
      TextWindow.WriteLine (Fahr)
    EndSub
    
    Sub Fahren
      TextWindow.Write("Enter Fahrenheit: " )
      Fahr = TextWindow.ReadNumber()
      Celsius = (Fahr - 32) * 5/9
      TextWindow.WriteLine(Celsius)
    EndSub


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Sunday, June 24, 2012 3:42 AM
    Answerer

All replies

  • Ok, it seems that I figured out how to make the Celsius work, but the Fahrenheit input still doesn't work. 

    begin:
    TextWindow.Write("Celsius or Fahrenheit? Enter format you want:")
    ch = TextWindow.Read()
    If ch = "Celsius" Then
      Goto Fahren
    If ch = "Fahrenheit" Then
      Goto celsius
    EndIf
    celsius:
    TextWindow.Write("Enter Celsius: ")
    c = TextWindow.ReadNumber()
    Fahr = (c*9)/5 +32
    TextWindow.WriteLine (Fahr)
    Fahren:
    TextWindow.Write("Enter Fahrenheit" )
    f = TextWindow.ReadNumber()
    celsius = (5/9)*(f-32)
    TextWindow.WriteLine(celsius)
    Endif 
    Goto begin

    Making progress....

    By the way.... are the equations correct? haha ;_;

    Sunday, June 24, 2012 2:15 AM
  • Your "EndIf" needs to be at the end of each "If" section. You will also need to "Goto begin" after the fahrenheit calculations right before the "fahren:" section. Your on the right track!
    Sunday, June 24, 2012 3:32 AM
  • Hi David Darrell

    I've made some tweaks to your code.

    Instead of asking a user to type such long words, why not just request a single key press? We're lazy!

    So, instead of TextWindow.Read(), use TextWindow.ReadKey()!

    Another nicey is to use subroutines for each specialized code you need.

    That is, a sub for Celsius and another one for Fahrenheit.

    See the code below:

    Ask:
    TextWindow.Clear()
    
    TextWindow.WriteLine("Press 'C' for Celsius or 'F' for Fahrenheit")
    ch = Text.ConvertToUpperCase( TextWindow.ReadKey() )
    
    If     ch = "C" Then
      Celsius()
    
    ElseIf ch = "F" Then
      Fahren()
      
    ElseIf ch = "Q" Then
      Program.End()
      
    Else
      TextWindow.WriteLine("Wrong option!!!")
      Sound.PlayBellRing()
      
    EndIf
    
    TextWindow.Pause()
    Goto Ask
    
    Sub Celsius
      TextWindow.Write("Enter Celsius: ")
      Celsius = TextWindow.ReadNumber()
      Fahr = Celsius * 9/5 + 32
      TextWindow.WriteLine (Fahr)
    EndSub
    
    Sub Fahren
      TextWindow.Write("Enter Fahrenheit: " )
      Fahr = TextWindow.ReadNumber()
      Celsius = (Fahr - 32) * 5/9
      TextWindow.WriteLine(Celsius)
    EndSub


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Sunday, June 24, 2012 3:42 AM
    Answerer