none
Presumably simple code, help needed. RRS feed

  • Question

  • Hello,  all. I am writing myself a personal program that will convert C° to F°, and vice versa, and it will ask a user to type either "Celsius" or "Fahrenheit" to execute the corresponding converter. So far, I only am able to code so you type Celsius to bring up that converter, but how can I have the program ask, for example, "Type Celsius or Fahrenheit to bring up the corresponding converter" and then depending on the answer, have the correct converter brought into execution? As said, I have only been able to have the program ask the question and then have it read either Celsius or Fahrenheit then execute only *1* converter..how do I program both depending on the answer?
    Thursday, August 20, 2009 4:31 AM

Answers

  • Your problem is caused by comparing the entry user directy.
    In the case that user enter Celcius, then it would go inside the Celcius IF bracket, but then when the entry is not Celsius, it would skip the Celsius loop, and expecting another user entry when you check with "If TextWindow.Read()". My sugestion you use just one TextWindow.Read() and put the entry in a variable like:
        conversion = TextWindow.Read()
    Then you can check the conversion with if statement, If (conversion = "Celsius") then ...

    Here's another recommendation on your code, it's a good coding practice if you need to use the same calculation twice, like you have one for the first calculation, then you put the same one on a loop. Think about another way so you don't have to put the same calculation twice.

    Another recommendation, in the case the entry of Fahrenheit or Celcius is mispelled, then there is no option other than quit the program. You can use an easier option, for instance, ask user to enter 1, to convert to Fahrenheit, 2 to convert to Celsius instead of spelling "Fahrenheit" because even slightly different case, using small case 'f' instead of upper case 'F', it would fail the condition.

    Thursday, August 20, 2009 8:39 PM

All replies


  • Here's a suggestion:

    You can use if statement to check the user entry. If it's Celsius then ask for digit to convert. Next you can use the same if statement to check if the entry is Fahrenheit, then do the similar conversion, but the other way, Fahrenheit to Celsius. This way you can be sure, when the entry is not Celsius or Fahrenheit, then it would not process anything. You can use while loop to go back to the beginning to check which conversion they want to do. 

    Does this help?
     

     


     

     

    Thursday, August 20, 2009 5:55 AM
  • Alright, thank you. It seems I now have it 99% down, my only problem is that when the user types in "Fahrenheit", they must do so two times before the Fahrenheit converter starts - this does not happen on the Celsius converter. I suppose I should post my code so it can be examined:

    TextWindow.Write("Type Celsius to start Cel Fahr conversion, or type Fahrenheit to start Fahr Cel conversion: ")
    If TextWindow.Read() = "Celsius" then
        Goto CelVarStart
        
        CelVarStart:
        TextWindow.Write("Please enter the temperature you wish to convert from Celsius to Fahrenheit: ")
    fahr = TextWindow.ReadNumber()
    fahrenheit = 9 * (fahr / 5) + 32
    TextWindow.WriteLine("Temperature in Fahrenheit is " + fahrenheit)
    CelVar:
    TextWindow.Write("Enter any other temperature you wish to convert from Celsius to Fahrenheit: ")
    fahr = TextWindow.ReadNumber()
    fahrenheit = 9 * (fahr / 5) + 32
    TextWindow.WriteLine("Temperature in Fahrenheit is " + fahrenheit)
    Goto CelVar
    Endif
    If TextWindow.Read() = "Fahrenheit" Then
          Goto FahrVarStart
          
          FahrVarStart:
          TextWindow.Write("Please enter the temperature you wish to convert from Fahrenheit to Celsius: ")
    fahr = TextWindow.ReadNumber()
    celsius = 5 * (fahr - 32) / 9
    TextWindow.WriteLine("Temperature in Celsius is " + celsius)
    FahrVar:
    TextWindow.Write("Enter any other temperature you wish to convert from Fahrenheit to Celsius: ")
    fahr = TextWindow.ReadNumber()
    celsius = 5 * (fahr - 32) / 9
    TextWindow.WriteLine("Temperature in Celsius is " + celsius)
    Goto Fahrvar
    EndIf
    Thursday, August 20, 2009 8:20 PM
  • Your problem is caused by comparing the entry user directy.
    In the case that user enter Celcius, then it would go inside the Celcius IF bracket, but then when the entry is not Celsius, it would skip the Celsius loop, and expecting another user entry when you check with "If TextWindow.Read()". My sugestion you use just one TextWindow.Read() and put the entry in a variable like:
        conversion = TextWindow.Read()
    Then you can check the conversion with if statement, If (conversion = "Celsius") then ...

    Here's another recommendation on your code, it's a good coding practice if you need to use the same calculation twice, like you have one for the first calculation, then you put the same one on a loop. Think about another way so you don't have to put the same calculation twice.

    Another recommendation, in the case the entry of Fahrenheit or Celcius is mispelled, then there is no option other than quit the program. You can use an easier option, for instance, ask user to enter 1, to convert to Fahrenheit, 2 to convert to Celsius instead of spelling "Fahrenheit" because even slightly different case, using small case 'f' instead of upper case 'F', it would fail the condition.

    Thursday, August 20, 2009 8:39 PM
  • Thank you so much, Khumara, everything is working as it should now! Also thank you for the other recommendations as well.
    Thursday, August 20, 2009 10:51 PM
  • I'm glad you can learn something through this exercise.
    Thursday, August 20, 2009 10:52 PM
  • Hi Alex,
    Here is a program that lets the user type the temperature followed by C, c, F or F.
    The program looks at the last character.  If it is C or c it treats it as Celsius.
    If it ends with F or f, it treats it as Fahrenheit.
    Then does the conversion.
    TextWindow.WriteLine("              Temperature Convertor")
    TextWindow.WriteLine("")
    TextWindow.WriteLine("Enter your temperature followed by F (for Fahrenheit)")
    TextWindow.WriteLine("or C (for Celsius) (ex: 17C)")
    reply = TextWindow.Read()
    
    ' Convert the user's input to uppercase
    reply = Text.ConvertToUpperCase(reply)
    
    ' Get the length of the user's input.
    len = Text.GetLength(reply)
    
    ' Copy all but last character
    deg = Text.GetSubText(reply, 1, len-1)
    
    ' If the last character is "C" or "c" convert to Fahrenheit
    If Text.EndsWith(reply, "C") Then
      degF = 9/5*deg + 32
      TextWindow.WriteLine(deg + " Celsius is " + degF + " Fahrenheit.")
    ElseIf Text.EndsWith(reply, "F") Then
      degC = (deg-32)*5/9
      TextWindow.WriteLine(deg + " Fahrenheit is " + degC + " Celsius.")
    EndIf
    
    

    Friday, August 21, 2009 12:58 PM