Thread: rewritten scanf for floats

  1. #1
    Registered User
    Join Date
    Sep 2002

    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)).


    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
    //We allocate one float to be used for storing the users input, pointed to by ptr			
    			return NULL;
    //prompt simply states to enter a float		
    //scan in a string, store it in 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
    			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.)
    //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.)
    //store the total of the two numbers in the dynamically allocated float
    return (float*)*ptr;
    don't be a two-bit user

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    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.

    Hope is the first step on the road to disappointment.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Visalia, CA, USA
    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
    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