Need Help Please joining two sub programs together?

Answered Need Help Please joining two sub programs together?

  • Sunday, August 19, 2012 6:55 AM
     
     

    Hi All

    Hi All  I am new to programming and this is some small basic code I wrote  and I would like to now join these two sub programs below together so that the user can choose to work in fahrenheit or celsius. I want to write the statement in the program below "TextWindow.Writeline("Enter Fahrenheit or Celisus:") and then when the user types in Fahrenheit or Celsius, the program "TextWindow.Read()" reads what the user typed in and  then goes to the correct part of the program to do the correct calculation? How do you make it so that the user can choose to work in fahrenheit or celsius? Any help would be really appreciated.

    TextWindow.BackgroundColor = "Black" 
    TextWindow.ForegroundColor = "Blue"
    TextWindow.WriteLine("Enter temperature in Fahrenheit and Press Enter: ")
    fahr = TextWindow.ReadNumber()
    celsius = (fahr-32) x 5/9
    TextWindow.WriteLine("Temperature in Celsius is " + celsius)

    TextWindow.Write("Enter temperature in celsuis and Press Enter: ")
    celsius = TextWindow.ReadNumber()
    fahrenheit =celsius*9/5 +32 
    TextWindow.WriteLine("Temperature in fahrenheit is " + fahrenheit)

All Replies

  • Sunday, August 19, 2012 9:56 AM
    Moderator
     
      Has Code

    Hi,

    You need to ask the question and depending on the answer call different code for celcius or farenheight.  There are several ways this could be done.

    First I would set a variable to say what unit type is being used, keep doing it until we get correct input, only checking first character to make input easier.

    'Units unset
    units = ""
    While (units <> "f" And units <> "c") 'repeat until we get a correct input
      TextWindow.Writeline("Enter Fahrenheit(F) or Celisus(C):")
      units = TextWindow.Read()
      'Just check the first letter in lower case
      units = Text.ConvertToLowerCase(units) 'lower case
      units = Text.GetSubText(units,1,1) 'first letter
    EndWhile

    Then depending on the result do different conversions using an If statement:

    If (units = "f") Then
      TextWindow.WriteLine("Enter temperature in Fahrenheit and Press Enter: ")
      fahr = TextWindow.ReadNumber()
      celsius = (fahr-32) * 5/9
      TextWindow.WriteLine("Temperature in Celsius is " + celsius)
    Else 'Must be celcius
      TextWindow.Write("Enter temperature in celsuis and Press Enter: ")
      celsius = TextWindow.ReadNumber()
      fahrenheit =celsius*9/5 +32 
      TextWindow.WriteLine("Temperature in fahrenheit is " + fahrenheit)
    EndIf
    You could also put the various bits in subroutines if you want and ask for repeat etc.
  • Sunday, August 19, 2012 2:42 PM
    Answerer
     
     Answered Has Code

    Hi CyberSpaceman!

    Since you have 2 subprograms, 1 to convert to Celsius and another to Fahrenheit, an alternative method, like litdev already mentioned, would be to place each one in 2 different subroutines. Like this:

    Sub ConvertToCelsius
      temp = (temp-32) * 5/9
    EndSub
    
    Sub ConvertToFahrenheit
      temp = temp * 9/5 + 32
    EndSub

    This way, you can call their "names" to do your bid anytime & anywhere within your program!

    Here's how I assembled your code:

    Sub ConvertToCelsius
      temp = (temp-32) * 5/9
    EndSub
    
    Sub ConvertToFahrenheit
      temp = temp * 9/5 + 32
    EndSub
    
    Sub RequestUnit
      unit = ""
      While unit <> "C" And unit <> "F" unit = Text.ConvertToUpperCase( TextWindow.ReadKey() ) EndWhile EndSub Sub AskExit TextWindow.ForegroundColor = "Magenta" TextWindow.WriteLine ("Hit 'Q' ou 'Esc' to quit or any other to restart..." + LF) key= Text.ConvertToUpperCase ( TextWindow.ReadKey() ) If key = "Q" Or key = ESC Then Sound.PlayClickAndWait() Program.End() EndIf EndSub LF = Text.GetCharacter(10) ESC = Text.GetCharacter(27) Restart: TextWindow.Clear() TextWindow.ForegroundColor = "DarkCyan" TextWindow.WriteLine("Hit 'C' to Celsius or 'F' to Fahrenheit conversion..." + LF) RequestUnit() TextWindow.ForegroundColor = "Red" TextWindow.Write("Enter temperature: ") TextWindow.ForegroundColor = "Green" temp = TextWindow.ReadNumber() If unit = "C" Then ConvertToCelsius() Else ConvertToFahrenheit() EndIf TextWindow.ForegroundColor = "Yellow" TextWindow.WriteLine(temp + "°" + unit + LF) AskExit() Goto Restart

    To study further, you can go in here, and download all course slides clicking here!

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

  • Monday, August 20, 2012 4:50 PM
     
     
    Thanks for the Help! Much Appreciated!
  • Friday, August 24, 2012 11:13 AM
     
     
    Hi All

    I am using GoToLoop code as a template and then putting different maths formulas into the code.

    how do I change While unit <> "K" And unit <>"N"  to While unit <> "KGF> And unit <> "N" ?

    How to change the code so that it can read words?

    TextWindow.BackgroundColor = "blue"
    TextWindow.ForegroundColor = "white"
    Sub ConvertToKilogramsforce
      force = force *101.971621298
    EndSub

    Sub ConvertToKiloNewtons
      force = force/101.971621298
    EndSub

    Sub RequestUnit
      While unit <> "K" And unit <> "N"
        unit = Text.ConvertToUpperCase(TextWindow.Readkey())
      EndWhile
    EndSub

    Sub AskExit
      TextWindow.WriteLine("Type 'Q' or 'Esc' to quit or any other to restart " +B)
      key=Text.ConvertToUpperCase(TextWindow.ReadKey())
      
      If key = "Q" Or key = ESC Then
        Sound.PlayClickAndWait()
        Program.End()
      EndIf
    EndSub

    B= Text.GetCharacter(10)
    ESC = Text.GetCharacter(27)

    Restart:
    unit=""
    TextWindow.Clear()
    TextWindow.WriteLine("Type 'K' and press enter to convert KiloNewtons per square metre," + B) 
    TextWindow.WriteLine("to Kilograms-force per square metre." + B)
    TextWindow.WriteLine("Type 'N' and press enter to convert Kilograms-force per square metre, " + B)
    TextWindow.WriteLine("to Kilonewtons per square metre." + B)
    RequestUnit()
    TextWindow.Write("Enter force:")
    force = TextWindow.ReadNumber()

    If unit = "K" Then
      ConvertToKilogramsforce()
    Else
      ConvertToKiloNewtons()
    EndIf

    TextWindow.WriteLine(force + unit + B)

    AskExit()
    Goto Restart




  • Friday, August 24, 2012 12:20 PM
    Answerer
     
     

    How do I change While unit <> "K" And unit <>"N"  to While unit <> "KGF" And unit <> "N" ?

    Hi!

    TextWindow.ReadKey() returns 1 character only; thus it would never return something like "KGF"!

    The advantage of using it is that the user is spared of the obligation to hit <Enter> every time for such a simple request!

    If you need the user to enter more than 1 character, you either use TextWindow.Read() or TextWindow.ReadNumber(). Then user hits <Enter> to indicate his/her input is finished.

    Perhaps all you want is to print "KGF" instead of just "K"?

    If so, just insert this extra line just after ConvertToKilogramsForce() between If & Else block:

    unit = "KGF"


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

  • Saturday, August 25, 2012 4:50 AM
     
     
    Thanks for the help GoToLoop
  • Monday, August 27, 2012 4:22 AM
     
     

    Hi All

    Do you know where there is a big library of small basic code which you could use as templates to write your own code?

    I am looking for lots of different small basic code which I can edit and write my own program, in that way learn small basic.

    TextWindow.BackgroundColor = "blue"
    TextWindow.ForegroundColor = "white"
    Sub ConvertToMechancialHorsePower
      power = power/745.69987158227
    EndSub
    Sub ConvertToWatts
      power = power *745.69987158227
    EndSub
    Sub RequestUnit
      While unit <> "M" And unit <> "W"
        unit = Text.ConvertToUpperCase( TextWindow.ReadKey() )
      EndWhile
    EndSub
    Sub AskExit
      TextWindow.WriteLine ("Hit 'Q' ou 'Esc' to quit or any other to restart..." + B)
      key= Text.ConvertToUpperCase ( TextWindow.ReadKey() )
      
      If key = "Q" Or key = ESC Then
        Sound.PlayClickAndWait()
        Program.End()
      EndIf
    EndSub
    B = Text.GetCharacter(10)
    ESC = Text.GetCharacter(27)
    Restart:
    unit = ""
    TextWindow.Clear()
    TextWindow.WriteLine("This program converts Watts to Mechanical Horsepower" +B)
    TextWindow.WriteLine("and Mechancial Horsepower to Watts." +B)
    TextWindow.WriteLine("James Watt (1736-1819) decided what a mechanical horsepower is." + B)
    TextWindow.WriteLine("One Mechanical Horsepower = a horse raising 149.6854821 kilograms of coal 30.48 metres in one minute." + B)
    TextWindow.WriteLine("Power = rate at which work is done." +B)
    TextWindow.WriteLine("One Mechanical Horsepower = 745.69987158227 Watts." +B)
    TextWindow.WriteLine("One Watt = 0.00134102209 Mechanical Horsepower." +B)
    TextWindow.WriteLine("There are 1000 watts (W) in one kilowatt (kW)." +B)
    TextWindow.WriteLine("Type 'M' and press enter to convert Watts to Mechancial Horsepower." +B)
    TextWindow.WriteLine("Type  'W' and press enter to convert Mechanical Horsepower to Watts." + B)
    RequestUnit()
    TextWindow.Write("Enter power: ")
    power = TextWindow.ReadNumber()
    If unit = "M" Then
      ConvertToMechancialHorsePower()
    Else
      ConvertToWatts()
    EndIf
    TextWindow.WriteLine(power + unit + B)
    AskExit()


  • Monday, August 27, 2012 9:31 AM
    Moderator
     
     
    I have a stack of samples here.  Go to the Other Resource Downloads section and download the Samples by clicking the icon.
  • Tuesday, August 28, 2012 1:06 PM
     
     

    Hi Litdev

    When I click on 'here' it says page can't be found.


  • Tuesday, August 28, 2012 4:16 PM
    Moderator
     
     
    Sorry link fixed (I missed off the http bit), it is http://litdev.hostoi.com
  • Wednesday, August 29, 2012 5:16 AM
     
     
    Thanks Litdev
  • Friday, August 31, 2012 3:48 AM
     
     

    Hi All

    How do I make this program below I wrote loop?

    TextWindow.BackgroundColor= "blue" 
    TextWindow.ForegroundColor = "white"
    TextWindow.WriteLine("This program works out compound interest:")
    TextWindow.WriteLine("Enter principle amount invested and press enter:")
    pa = TextWindow.ReadNumber()
    TextWindow.WriteLine("Enter interest per year earned and Press Enter:")
    i= TextWindow.ReadNumber()
    TextWindow.WriteLine("Enter number of years your money has been invested:")
    years= TextWindow.ReadNumber()
    FinalInvestmentValue=pa *(1+i)*(years)
    TextWindow.WriteLine("Final investment value is " +FinalInvestmentValue)


  • Friday, August 31, 2012 3:52 AM
     
      Has Code
    TextWindow.BackgroundColor= "blue" 
    TextWindow.ForegroundColor = "white"
    TextWindow.WriteLine("This program works out compound interest:")
    Loop:
    TextWindow.WriteLine("Enter principle amount invested and press enter:")
    pa = TextWindow.ReadNumber()
    TextWindow.WriteLine("Enter interest per year earned and Press Enter:")
    i= TextWindow.ReadNumber()
    TextWindow.WriteLine("Enter number of years your money has been invested:")
    years= TextWindow.ReadNumber()
    FinalInvestmentValue=pa *(1+i)*(years)
    TextWindow.WriteLine("Final investment value is " +FinalInvestmentValue)
    GoTo Loop()

    Or at least something like that. :)

    -Noah J. Buscher "Nothing is Impossible Until Proven Impossible."

  • Friday, August 31, 2012 3:55 AM
     
     
    BTW... Please add this as an answer if it helps you. Thanks! :)

    -Noah J. Buscher "Nothing is Impossible Until Proven Impossible."

  • Friday, August 31, 2012 5:00 AM
     
     

    Thanks for the help Noah!

    Thanks for the help Noah! How do I give the user the option to end the loop ie; exist the program in the code below.

    TextWindow.BackgroundColor= "blue" 
    TextWindow.ForegroundColor = "white"
    TextWindow.WriteLine("This program works out compound interest:")
    start:
    TextWindow.WriteLine("Enter principle amount invested and press enter:")
    pa = TextWindow.ReadNumber()
    TextWindow.WriteLine("Enter interest per year earned and Press Enter:")
    i= TextWindow.ReadNumber()
    TextWindow.WriteLine("Enter number of years your money has been invested:")
    years= TextWindow.ReadNumber()
    FinalInvestmentValue=pa *(1+i)*(years)
    TextWindow.WriteLine("Final investment value is " +FinalInvestmentValue)
    GoTo start 

  • Friday, August 31, 2012 6:23 AM
    Answerer
     
      Has Code

    Hi again CyberSpaceman!

    In the temperature conversion program I've helped you 1st, there is a subroutine called AskExit. It's a reusable code, a snippet!   :P

    You just need to transfer it to any console-based program, and call AskExit(), to ask the user whether he prefers to go another round or simply quit.

    The exact method which makes it happen is -> Program.End()

    Any further doubts, just ask! Or take a peek at studying how my example there was set up.   ^_^

    LF  = Text.GetCharacter(10)
    ESC = Text.GetCharacter(27)

    Restart:

    AskExit()

    Goto Restart

    Sub AskExit TextWindow.ForegroundColor = "Magenta" TextWindow.WriteLine ("Hit 'Q' ou 'Esc' to quit or any other to restart..." + LF) key= Text.ConvertToUpperCase ( TextWindow.ReadKey() ) If key = "Q" Or key = ESC Then Sound.PlayClickAndWait() Program.End() EndIf EndSub


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

  • Friday, August 31, 2012 9:39 AM
     
     

    Thanks GoToLoop for the help!

    LF  = Text.GetCharacter(10)
    ESC = Text.GetCharacter(27)
    Restart:
    TextWindow.BackgroundColor="Blue"
    TextWindow.ForegroundColor="White"

    TextWindow.WriteLine("Enter principle amount invested and press enter:")
    pa = TextWindow.ReadNumber()
    TextWindow.WriteLine("Enter interest per year earned and Press Enter:")
    i= TextWindow.ReadNumber()
    TextWindow.WriteLine("Enter number of years your money has been invested:")
    years= TextWindow.ReadNumber()
    FinalInvestmentValue=pa *(1+i)*(years)
    TextWindow.WriteLine("Final investment value is " +FinalInvestmentValue)
    AskExit()
    Goto Restart
    Sub AskExit
      TextWindow.ForegroundColor = "Magenta"
      TextWindow.WriteLine ("Hit 'Q' ou 'Esc' to quit or any other to restart..." + LF)
      key= Text.ConvertToUpperCase ( TextWindow.ReadKey() )

      If key = "Q" Or key = ESC Then
        Sound.PlayClickAndWait()
        Program.End()
      EndIf
    EndSub