#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    3
    Rep Power
    0

    How do I fix my problem?


    Hi I am new here and programming.

    I am making a program for temperature conversion now.

    I have this for input a temperature(rhtml)

    <form action = "result" method = "post">
    <tr>
    <td> Celsius to Fahreheit: </td>
    <td> <input type = "text" id = "CtempF" name = "CtempF"
    size = "5" /></td></br>
    <tr/>
    <input type = "submit" name = "CtempF" value = "Submit" />
    <input type = "reset" value = "Clear Form" />
    </br>
    </form>
    --------------------------------------------------------

    And I have a method for conversion(.rb)

    def result
    @CtempF=params[:CtempF].to_f

    if @CtempF > -459.0
    @convertedCtoF=((9.0 * @CtempF)/5.0)+32.0
    @convertedCtoF=sprintf("%5.1f", @convertedCtoF)
    elsif @CtempF < -459.0
    @convertedCtoF=sprintf(" 'INVALID! THE NUMBER SHOULD BE GREATER THAN -459'")
    else
    @convertedCtoF=sprintf(" 'INVALID! SHOULD BE NUMERIC'")
    end
    end
    ....................................................
    Finally I have this for the output the result(.rhtml)

    Celsius to Fahreheit is: <%= @convertedCtoF %> <br/>


    If I enter 22 into the textbox, it works fine.
    If I enter -666 into the textbox, it also works fine.
    But if I enter "aaa", the output shows "Celsius to Fahreheit is: 32.0".

    I don't know where the 32.0 comes from, and how to fix it.

    Could anybody teach me how to fix it?

    Thank you
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    please learn to use code tags; they make your post infinitely more readable.
    The reason this is happening can be described with an example:
    Code:
    irb(main):001:0> s = "aaa".to_f
    => 0.0
    So your check passes...
    You can do a couple of things to avoid this, but the following is probably how I would go about it
    Code:
    def foo(v)
       begin
          Float(v)
       rescue
          "Invalid Argument"
       end
    end
    And the result:
    Code:
    irb(main):020:0> foo 123
    => 123.0
    irb(main):021:0> foo "123"
    => 123.0
    irb(main):022:0> foo "aaa"
    => "Invalid Argument"
    Realize that usually you could do something along the lines of object.kind_of? Numeric, but since you know you are getting a string, that would never work. There might be a more elegant solution, but off the top of my head, I am not coming up with it...

    Comments on this post

    • netytan agrees : can't think of a better way either
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    3
    Rep Power
    0
    Hi, thank you for your reply.
    I understand why my program outputted the number after I entered the string.
    And I tried to modify my code. But I cannot change it.
    Could you please show me how to change the result method?

    Thank you
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    Originally Posted by adhe
    And I tried to modify my code. But I cannot change it.
    I'm not sure what you mean by this. Do you mean that you can not edit the file? If you can edit the file, what is preventing you from changing the code?
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    3
    Rep Power
    0
    Originally Posted by L7Sqr
    I'm not sure what you mean by this. Do you mean that you can not edit the file? If you can edit the file, what is preventing you from changing the code?
    I don't know how to do like

    def result(v)
    -----
    -----
    -----
    end


    and how to use "v" inside the method.

    Thank you
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    This really breaks down to the basics of programming (not just with Ruby).

    If you have access to irb (the ruby interactive environment) I would suggest that you try to write some things there as you gain an understanding of just how they work.

    To help with how to use function calls and arguments to those functions, try the following goolge terms:
    subroutine, function, method, procedure, subprogram
    The first one there should lead you direct to wikipedia which will allow you to explore further.

    Hope that helps...
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog

IMN logo majestic logo threadwatch logo seochat tools logo