Thread: help with floating point input

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116

    help with floating point input

    I'd like to write a program that acts like a really basic cash machine.
    Now, obviously, when people buy things from a store the item is usually not a whole number, it's going to be a decimal number.
    I was originally going to use the getchar() function for input but I thought about it a little bit and realized that getchar() gets the next character (number I guess in this case). So if I used getchar() it would cause the variable to act something like this:

    Code:
    int c, price;
    
    while((c = getchar()) != EOF)
    {
            price = c;
    }
    so if the user entered 3.21
    price = 3
    price = .
    price = 2
    price = 1

    Is this correct? If so what function should I use to get input?
    Thanks in advance.
    -M.I.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    fgets() is a good method to use. It will get a string, so you'll need to allocate a char array big enough to hold whatever length you feel is appropriate.

    Then, you can parse the value and validate it, and then convert it. (or convert as you validate - however you want to go about it.)

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    Yeah, I thought about using that (mostly because I'm trying to stay away from scanf() like the plague) but I wasn't sure how to go about parsing the information from an array into a floating point number. Any suggestions?

    edit: XD i found this new thing, it's called the search function.
    Let me see what I can find with that and then I'll ask for more help
    Last edited by MikeyIckey; 03-07-2008 at 08:56 PM.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    strtod should help
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    yup, I was looking at atof or strtod.
    atof looks easier to use, but as I understand it atof has been deprecated by strtod so I should probably use that one, huh?
    Thanks!

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    I'm just having a problem understanding how to use the strtod function.
    I know it takes 2 arguments but I don't understand what those two arguments need to be.

    here's what I have so far.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void welcome(void);
    double input(char array[], double scan);
    
    int main()
    {
    	double total, newItem;
    	char basket[10];
    
    	welcome();
    	input(basket, newItem);
    
    	return 0;
    }
    
    void welcome(void)
    {
    	printf("Welcome to RandoMart, enter the price of the first item. \n");
    }
    
    double input(char array[], double scan)
    {	
    	fgets(array, 10, stdin);
    	scan = strtod(array, &array);
    }
    does that last line look like it will work properly or am I totally confused on how I should implement strtod?

    Thanks again for the help guys.
    M.I.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You'll want a completely separate char ** to be the second argument to strtod; (1) if I understand restrict at all (hint: I don't), the first and second argument can't refer to the same memory, and (2) afterwards, you'll want to compare the second argument to array; if they're the same, then the read/conversion failed and you'll have to do it again.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Actually return your variable to main.

    tabstop is correct. The purpose of endptr is to be assigned to the place where conversion stopped. You'll want to be certain that you stopped in an acceptable location. Here's an example function that's pretty well commented:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    double
    input(char array[], unsigned long array_size);
    
    double
    input(char array[], unsigned long array_size)
    {
        double scan;
        if( fgets(array, array_size, stdin) != NULL ) {
            char * endptr = NULL;   /** for tracking the end of conversion **/
            scan = strtod(array, &endptr); /** pass the pointer itself **/
            if( scan != 0.0 && ( *endptr == '\n' || *endptr == '\0' ) ) {
                /** in order for this to be true, the user entered 
                 ** something other than zero, and the endptr stopped at either
                 ** a zero terminator or a newline ... **/
                return scan;
            }
        }
        return 0.0; /** error **/
    }
    Just so that you can see how effective it is, I did some simple testing:
    Code:
    Enter a new item:
    12.4
    The string recieved was "12.4
    "
    newItem is 12.4
    Enter a new item:
    f42.5
    The string recieved was "f42.5
    "
    newItem is 0
    Enter a new item:
     0.012g
    The string recieved was " 0.012g
    "
    newItem is 0
    Enter a new item:
        12.4
    The string recieved was "    12.4
    "
    newItem is 12.4
    Enter a new item:
    one
    The string recieved was "one
    "
    newItem is 0
    Press any key to continue . . .
    There are some flaws, like how it is perfectly acceptable to have whitespace before the number. But with a little thought, you can be very stringent. If all else fails, try inventing some custom validation.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    Wow, thanks a lot for the info guys.
    And Citizen, thanks especially for the test output, it's really given me something to think about.
    Again, I really appreciate the help.
    I'll be back with the results and my code.
    -M.I.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Floating point #'s, why so much talk about it?
    By scuzzo84 in forum C Programming
    Replies: 5
    Last Post: 09-20-2005, 04:29 PM
  2. floating point binary program, need help
    By ph34r me in forum C Programming
    Replies: 4
    Last Post: 11-10-2004, 07:10 AM
  3. floating point operators
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2003, 07:53 PM
  4. 2 questions about floating point and %
    By ams80 in forum C Programming
    Replies: 2
    Last Post: 08-14-2002, 10:55 AM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM