Discuss How do I fix my problem? in the Ruby Programming forum on Dev Shed. How do I fix my problem? Ruby and Ruby on Rails programming forum covering Ruby Tips and Tricks, Best Practices, and agile development with Ruby on Rails.
Posts: 3
Time spent in forums: 4 h 24 m 8 sec
Reputation 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.
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...
__________________
True happiness is not getting what you want, it's wanting what you've already got.
Posts: 3
Time spent in forums: 4 h 24 m 8 sec
Reputation 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?
Posts: 989
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
Quote:
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?
Posts: 3
Time spent in forums: 4 h 24 m 8 sec
Reputation Power: 0
Quote:
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?
Posts: 989
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
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.