Thread: Checking for a number

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    204

    Question Checking for a number

    So I have a function that is called everytime the user inputs something, to check whether it is a number or, includes letters aswell. But I want the user to be able to input a decimal, such as 1.234 or 0.4354 etc. I also want the user to be able to input negative numbers, and fractions.

    Here is my function at the moment,

    Code:
    int is_digit(char* digit)
        {
             int len=strlen(digit);
             int i;
             for(i=0;i<len;i++)
            {
                 if(isdigit(digit[i])==0)
                 {
                     if ((digit[i]) == ".")
                     {
                        
                     }
                     else
                     {
                       return -1;
                     }
                  }
            }
    
         return atoi(digit);
        }
    It should only be checking to see whether the letter is a decimal point, and if so allow it, but it doesnt work. I am going to also make if statements checking whether the first letter is a - sign and allow that in a similar manner.

    Please can someone sport whats wrong.

    Thanks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    single quotes here
    Code:
    if ((digit[i]) == ".")
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    Thanks and what about
    Code:
      if ((digit[i]) == '-')//checking whether the first symbol, that is a negative sign
                         {
                         }
                         else
                         {
                             return -1;
                         }
    It worked for the decimal, but not for minus sign for some reason?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What's the full code? It looks like you are returning -1 if the first character is not a minus. It could validly be not minus, right?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    the full loop is:
    Code:
    for(i=0;i<len;i++)
            {
                 if(isdigit(digit[i])==0)
                 {
                     if (i==0)//checking to see whether first character is 0, before checking if its a minus.
                     {
                         if ((digit[i]) == '-')//checking whether the first symbol, that is a negative sign
                         {
                         }
                         else
                         {
                             return -1;
                         }
                     }
    
                     if ((digit[i]) == '.')//checking whether the symbol that is not a number, is a decimal point.
                     {
    
                     }
                     else
                     {
                       return -1;
                     }
                  }
            }
    thanks for your help

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like you forgot to handle the "normal" cases where the character is a numeral.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
                        if ((digit[i]) == '-')//checking whether the first symbol, that is a negative sign
                         {
                         }
                         else
                         {
                             return -1;
                         }
    Would it not be easier to write (and read):
    Code:
    if ((digit[i]) != '-')//checking whether the first symbol, that is a negative sign
    {
          return -1;
    }
    ?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What's the point in checking for a decimal point if you are converting to an int? Also, how do you know if it's returning "bad negative number encountered", or if they actually entered "-1"?


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  2. Stone Age Rumble
    By KONI in forum Contests Board
    Replies: 30
    Last Post: 04-02-2007, 09:53 PM
  3. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 08:14 PM
  4. Perfect number...
    By Argo_Jeude in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 01:53 PM
  5. [C++/WinAPI] Checking number of allocated memory
    By jagi in forum C++ Programming
    Replies: 2
    Last Post: 03-28-2005, 06:10 PM