Thread: Error Trap for a 2nd decimal

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    3

    Error Trap for a 2nd decimal

    I am wanting to add a second portion to a validation function for a float to trap if a 2nd decimal point is accidently entered.
    Code:
    int sValidFloatString(char *chFloat)
    {
     	// Declare Variables for Function
     	float flValidEntry = 0.0;
     	int i = 0;
     	char chStringEnd = '\0';
     	char chDot = '.';
     
     	//Check each chararacter in string for validity
     	while(chFloat[i]!=chStringEnd)
     	{
      		//If entered character not a valid number, return invalid and exit function
      		if(isdigit(chFloat[i])!=0 || (chFloat[i]==chDot))
      		{
    			//Valid entry
    			flValidEntry = 1;
    		}
      		else
      		{
       		//Invalid entry, end function
       		flValidEntry = 0;
    		break;
      		}
     
      		//Movenext
      		i++;
     	}
     
        	return flValidEntry;
    }
    // ********** End Function ************
    
    Example:
    User input: 1.23.2
    Output: Invalid Entry
    Right now it just ignores the 2nd decimal point and the following digit.

    Thank you in advance

    ropewrench

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you use a counter to keep track of how many decimal places you have come across? If it's more than one, stop looking and spit out an error.


    Or you could always just use the string functions...
    Code:
    char *ptr1, *ptr2;
    
    ptr1 = strchr( chFloat, '.' );
    ptr2 = strrchr( chFloat, '.' );
    
    if( ptr1 == ptr2 && ptr1 )
        printf("Has one decimal place.\n");
    else
        printf("Has more than one, or perhaps none.\n");

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

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    3
    Quzah,
    Thank you for the suggestions. I have not quite gotten it yet.
    Code:
    // ********** Check for Valid Float Entry Function ***********
    // Function checks users entry to see if it is valid integer value
    
    
    int sValidFloatString(char *chFloat)
    {
     	// Declare Variables for Function
     	float flValidEntry = 0.0;
     	int i = 0;
     	char chStringEnd = '\0';
     	char chDot = '.';
    	int count = 0;
     	//Check each chararacter in string for validity
     	while(chFloat[i]!=chStringEnd)
     	{
      		//If entered character not a valid number, return invalid and exit function
      		if(isdigit(chFloat[i])!=0 || (chFloat[i]==chDot) && (chFloat[count] == chDot))
      		{
    			if(chFloat[count]>1)
    			{
    				flValidEntry = 0;
    				break;
    			}
    			else
    			{
    				//Valid entry
    				flValidEntry = 1;
    			}
    			//Valid entry
    			flValidEntry = 1;	
    		}
      		else
      		{
       		//Invalid entry, end function
       		flValidEntry = 0;
    		break;
      		}
     
      		//Movenext
      		i++;
      		count++;
     	}
     
        	return flValidEntry;
    }
    // ********** End Function ************

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You just need to turn into code how you would look at it yourself:
    Code:
    if this character is not a digit
        if this character is a dot
            increment dot count
        else
            bad character
        if dot count greater than one
            bad format
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    3
    Quzah,
    Thank you for your help. I got it to work pretty well. It is not the prettiest code in the world but it did the job. I would like to thank everyone on this forum, it has proven to be invaluable.
    Code:
    // ********** Check for Valid Float Entry Function ***********
    // Function checks users entry to see if it is valid integer value
    
    
    int sValidFloatString(char *chFloat)
    {
     	// Declare Variables for Function
     	float flValidEntry = 0.0;
     	int i = 0;
     	char chStringEnd = '\0';
     	char chDot = '.';
    	int count = 0;
    	char *chTempDot;
     	//Check each chararacter in string for validity
     	while(chFloat[i]!=chStringEnd && chFloat[count]!=chStringEnd)
     	{
     		/*if (chFloat == chDot)
     		{
     			chTempDot = chFloat;
     			
     		}*/
      		//If entered character not a valid number, return invalid and exit function
      		if(isdigit(chFloat[i])!=0 || (chFloat[count] == chDot && count <=1))
      		{	
    			//Valid entry
    			flValidEntry = 1;	
    			
    		}	
      		else
      		{
       		//Invalid entry, end function
       		flValidEntry = 0;
    		break;
      		}
     
      		//Movenext
      		i++;
      		count++;
     	}
     
        	return flValidEntry;
    }
    // ********** End Function ************

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 03:52 AM
  2. hex to binary,hex to decimal
    By groovy in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 02:14 AM
  3. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 07:52 AM
  4. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 11:07 PM