Thread: rewritten scanf for floats

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    16

    Question rewritten scanf for floats

    Hello all,
    I am currently working on a library of functions that I will be able to use in place of scanf(), simply because scanf() is unbelievably stupid. Anyways, I have code written for ints, longs, doubles, chars, but my code for floats is giving me problems. It almost comes out right every time, but it is imprecise in its calculation of the number that the user enters. Please help (especially if you understand the code(and doubly especially if you know the answer)).

    Thanks,
    monkey_c

    Code:
    float *getFloat(float **ptr, const char*prompt){
    	
    	float t1=0, t2=0,placer=0;
    	char input[1024];
    	int i=0,flag=0, whereDot=1;
    	
    	
    	puts("\n\n\n\n\nThis is getFloat\n\n\n\n\n");
    
    //if ptr is not NULL we free the memory and NULL out the pointer
    	if(*ptr!=NULL){
    			(void)free(*ptr);
    			*ptr=NULL;
    			}
    		
    //We allocate one float to be used for storing the users input, pointed to by ptr			
    		if((*ptr=(float*)calloc(sizeof(float),1))==NULL)		
    			return NULL;
    //prompt simply states to enter a float		
    		do{
    		puts(prompt);
    //scan in a string, store it in input	
    		fscanf(stdin,"%s",input);
    //validate all characters to make sure input is really a float
    		for(i=0, flag=0;input[i]!='\0';i++){
    //find the decimal place
    			if(input[i]=='.')
    				whereDot=i;			
    			if(((input[i]<'0')||(input[i]>'9'))&&(input[i]!='.')&&(input[i]!='-')){			
    				flag=1;
    				break;
    				}
    		}
    		if(flag==1)
    			fprintf(stderr,"\n!!Each digit in your float must contain a value between 0 and 9!!");
    		}while (flag==1);
    
    //traverse the array right to left starting at the number before the
    //decimal point (the ones column) and multiply the value in each element
    //by placer (placer is the value of that column,1's,10's,100's,etc.)
    		for(placer=1,i=whereDot-1;i>=0;i--,placer*=10)
    		t1+=(input[i]-48)*placer;
    //traverse the array left to right starting at the number after the
    //decimal point (the one tenth's column) and multiply the value in each element
    //by placer (placer is the value of that column,1/10's,1/100's,1/1000's,etc.)
    		
    		for(i=whereDot+1,placer=.1;input[i]!='\0';placer*=.1,i++){
    		t2+=(input[i]-48)*placer;
    		}
    //store the total of the two numbers in the dynamically allocated float
    	**ptr=(float)((float)t1+(float)t2);
    return (float*)*ptr;
    	
    }
    don't be a two-bit user

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're probably just enocountering the limitation that floating point numbers themselves have. Floating point numbers, due to the way they're stored, end up being imperfect for high precision. There is no way around this, other than to not use them. I'm sure if you search the board you'll find loads on the topic.

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

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    scanf is indeed stupid. That is why I never use it. Even if you re-write it the end result should be something very similar.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    16
    So why do you use scanf() to read a string?
    I used scanf under the auspices that my error checking would make up for its shortcomings.

    > if(*ptr!=NULL){
    Who said my pointer was pointing at memory which can be freed?
    This code is only to be used for one of my assignments in my current class. I will obviously adapt it to suit my needs in future apps. There are several other things which would need to be cleaned up before I would give it to someone else to use or even for me to use on a large scale.

    Thank you both for the advice on the precision of floats as well as the error checking failures of my code. Again, thanks
    don't be a two-bit user

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  2. Replies: 2
    Last Post: 02-20-2005, 01:48 PM
  3. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 AM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM