School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 309,245 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,638 people online right now. Registration is fast and FREE... Join Now!




Distinguish between string and number?

 

Distinguish between string and number?

mmzdaniel

27 Apr, 2009 - 07:54 AM
Post #1

New D.I.C Head
*

Joined: 17 Jan, 2009
Posts: 5

Ive been trying to make an app, that takes an input... what the app really does, doesn't really matter as my problem is... how do I make my program to know if the user entered a string or a number? so far i made this:
CODE
#This function is just a factorial function.
def fact(n)
    a = 1
    b = 1
    while a <= n
        b = b * a
        a += 1
    end
end
#And this is the function I'm talking about. Its the best i could think of...
def num?(n)
    true if (fact(n) != 0) rescue false
end

problem is, the program uses numbers and 0 should be considered a number as well... with this function its not possible.
What I'm asking for is an efficient function to give me a true value if the user got a number and false if its a string...
Pseudocode would be preferable but the function alone would be fine too i guess...
Thanks.

P.S.
This is a ninja ph34r.gif

This post has been edited by mmzdaniel: 27 Apr, 2009 - 07:56 AM

User is offlineProfile CardPM
+Quote Post



Fast ReplyReply to this topicStart new topic

Time is now: 11/26/09 09:39AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month