floating point function

This is a discussion on floating point function within the C++ Programming forums, part of the General Programming Boards category; I am tring to make a function to check to make sure the floating piont value is a valid number. ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    26

    floating point function

    I am tring to make a function to check to make sure the floating piont value is a valid number. i am having some problem getting it to work correctly.
    any help would be great.
    thanks
    Sal
    Code:
    #include <stdio.h>
    #include "floatgetnum.h"
    /* prototype */
    
    float getnum();
    int main()
    {
    
      float retval=0;
      printf("Please enter a floating point number");
        do
        {
          retval = getnum();
           printf("The number was %f\n", retval);
    
        }while (retval !=-1);
    
        return 0;
    }
    
    float getnum()
    {
      char ch;
      int nflag,dflag,number,pos=0;
      int dplace=1;
      ch=getchar();
      while (ch!='\n'&&ch!=' ')
        {
          switch (ch)
            {
            case '-':
              nflag=1;
              pos++;
              break;
            case '+':
              pos++;
              break;
            case '.':
              if (dflag==1)
                {
                  printf ("Invaild user input\n");
                  return EOF;
                }
              else
                {
                  dflag++;
                  pos++;
                }
               break;
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
     number*=10;
              number+=atoi(&ch);
              if (dflag==1)
                {
                  dplace*=10;
                }
              pos++;
              break;
            }
          ch=getchar();
          if (nflag==1)
            {
              number*=-1;
            }
          if (dflag==1)
            {
              number=number/dplace;
            }
        }
          return number;
    }/*function*/

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Is this C or C++? If it is C you might get better responses on the C board, if it is C++ why are you using C tools?

    What problems are you having? Do you get compile errors? Do you get different answers than you expect? What input do you give, what output do you expect and what output do you get?

    One issue that I see is that you are using atoi on a single character. The atoi function expects a pointer to a null terminated array of characters, you are passing a pointer to a single character. If you want to convert a single character to an int, just subtract the '0' character:
    Code:
    number += ch - '0';

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You never initialize the variable number in the getnum() function.
    another thing
    Code:
    number+=atoi(&ch);
    you cannot just take the address of a char variable and pass it to atoi(). Taking the address makes it a char * but doesn't make it a zero terminated string.
    use
    Code:
    number += ch - '0';
    instead
    Kurt

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    it is tring to check for a valid floating number 12.34 , .1234, -1234
    give back that input if valid return EOF if invaild.
    i was told you could use
    number+=atoi(&ch)
    to send ch to atoi converting if need be.
    Last edited by Sal79; 04-17-2007 at 12:49 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You can't do it that way in your program for the reasons we mentioned.

    Are you familiar with how a C style string works? Do you understand the difference between an array of characters and a single character?

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    no. You can't.
    Kurt
    EDIT: Sh.., too slow again. It's a shame. Twice in the same thread
    Last edited by ZuK; 04-17-2007 at 12:49 PM.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    the program works the same with both
    it seems i need a little help with the difference array of characters and single ones.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > int nflag,dflag,number,pos=0;
    Wouldn't it make more sense to declare number as a float or double?
    Code:
      double number;

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> the program works the same with both
    Does that mean it still doesn't work? What problems are you having? Do you get different answers than you expect? What input do you give, what output do you expect and what output do you get?

    >> it seems i need a little help with the difference array of characters and single ones.
    For now, you can trust us that number += ch - '0'; is the correct version.

    A string is just a sequence of 0 or more characters. In C, the way to store a string is with an array. However, you cannot always tell the size of an array just by looking at it. That means that in order to do real work with strings stored in character arrays, you would have to pass around the size to functions that work on strings. This would be cumbersome, so they used a trick to remember the size. They add a null character that signals the end of the string.

    So when you use a function that expects a C style string (like atoi), it wants a pointer to an array of characters. It also wants the important characters to end with a null character: '\0'. For example, "123" is a string. It is represented as an array of 4 characters, '1', '2', '3' and '\0'. The atoi function gets a pointer to the '1', then keeps looking until it finds the '\0'. It then converts "123" to the number 123.

    In your code, you are reading in a single character only. If the user types 123, then you read in '1' and process it. Since atoi is not meant for a single character, and since there is no '\0' after the '1', then atoi won't work for you. Instead you use the other trick we showed you.

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    if i enter a number '1', '2', '3' it reads and outputs like a float 123.00000
    after that just locks up unitl i exit
    i guess the real problem is that i want to pass an array of characters '1', '.', '2', '3'

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You might want to initialize dflag too:
    Code:
    int dflag = 0;

  12. #12
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    the whole line is set to 0

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> i guess the real problem is that i want to pass an array of characters '1', '.', '2', '3'
    I don't think you want to pass an array of characters. Your algorithm is setup to read one character at a time.

    >> the whole line is set to 0
    No, that's not how initializations work, you have to initialize each one individually.

    Can you show us your new code, with the proper initializations, with number declared as a float, and with ay other changes you've made?

  14. #14
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    It will return a the value you enter but will not put the decimal in the right spot.
    if i enter 1234 i get 1234.000000
    if i enter 12.34 i get 1234.000000
    Code:
    #include <stdio.h>
    #include "floatgetnum.h"
    /* prototype */
    
    float getnum();
    int main()
    {
    
      float retval=0;
          do
        {
          printf("Please enter a floating point number");
          retval = getnum();
           printf("The number was &#37;f\n", retval);
    
        }while (retval !=EOF);
    
        return 0;
    }
    
    float getnum()
    {
      char ch;
      int pos=0;
      int dplace=1;
      int nflag=0;
      int dflag=0;
      float number=0;
      ch=getchar();
        while (ch!='\n'&&ch!=' ')
        {
          switch (ch)
            {
            case '-':
              nflag=1;
              pos++;
              break;
            case '+':
              pos++;
              break;
            case '.':
              if (dflag==1)
                {
                  printf ("Invaild user input\n");
                  return EOF;
                }
              else
                {
                  dflag+=1;
                  pos++;
                }
              break;
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
             case '6':
            case '7':
            case '8':
            case '9':
              {  number*=10;
              number += ch - '0';
              break;
              }
    
              if (dflag==1)
                {
                  dplace*=10;
                }
              pos++;
              break;
            }
          ch=getchar();
        }
      if (nflag==1)
        {
          number*=-1;
        }
    if (dflag==1)
        {
          number = number / dplace;
        }
    
      return number;
    }/*function*/

  15. #15
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    TBH, I would just read the number up to the '.' as an integer. I would read what comes after it as a second integer. So if the user enters 12.34, I would have 2 ints: 12 and 34.

    Then using basic math, combine them to a float: 12 + ((float)34/100) = 12.34

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. floating point number comparison
    By lehe in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2009, 07:11 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21