Thread: Validate Float Logic?

  1. #1
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    Question Validate Float Logic?

    I spent the evening trying to figure a good way to validate user input of a floating point number from a string . The following code snippet works, but seems complicated to me. I am sure there is probably a more efficient way. If so could someone please give me some constructive criticism or pointers.


    Code:
    ////////////////////////////////////
    // Validate floating point number.//
    ////////////////////////////////////
    
    int Decimal = 0;
    
    for (int i = 0; i < nLen; i++ )
    {
    if ( !isdigit(pbuffer[i]))
    {
    		if ( Decimal == 0 )
    		{
    			if( pbuffer[i] == '.' )
    			{
    				Decimal = 1;
    				continue;
    			}
    		}
    		MessageBox (NULL, TEXT ("Enter a floating point number."), 
    			TEXT ("Invalid input!"), MB_ICONINFORMATION) ;
    		return TRUE;	// break out of switch statement.
    }
    }
    
    // pbuffer is valid so do calculations.
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    http://www.dinkumware.com/manuals/re...ib.html#strtod

    This might help.

    EDIT: The link only works sometimes. Once you get in there, look up strtod if it doesn't automatically bring you to it.

  3. #3
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    Thumbs up Thanks for the info.

    Thanks for the info. I'll try it out.
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM