Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 6th, 2008, 09:55 AM
adhe adhe is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 3 adhe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Could anybody teach me how to fix it?

Thank you

Reply With Quote
  #2  
Old June 6th, 2008, 12:53 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 989 L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
Send a message via AIM to L7Sqr
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

Reply With Quote
  #3  
Old June 6th, 2008, 02:56 PM
adhe adhe is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 3 adhe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Thank you

Reply With Quote
  #4  
Old June 6th, 2008, 05:36 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 989 L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
Send a message via AIM to L7Sqr
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?

Reply With Quote
  #5  
Old June 6th, 2008, 06:10 PM
adhe adhe is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 3 adhe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?


I don't know how to do like

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


and how to use "v" inside the method.

Thank you

Reply With Quote
  #6  
Old June 6th, 2008, 11:24 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 989 L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
Send a message via AIM to L7Sqr
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...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > How do I fix my problem?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap