ASCII to float

This is a discussion on ASCII to float within the C Programming forums, part of the General Programming Boards category; Hello again allmighty internet community: I'm a little bit stuck; I'm writting a program that will convert a number entered ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    10

    ASCII to float

    Hello again allmighty internet community: I'm a little bit stuck; I'm writting a program that will convert a number entered as a string into its correpsonding floating point representation. To do this I'm using the atof() function, which as I understan stands for ASCII to float. After watching the variables everything is ok except the converted variable which always gets an annoying 0.000000. Here's the code I've managed to cook so far:

    Code:
    #include<conio.h>
    #include<stdio.h>
    #include<string.h>
    
    float Convert(char data[128]);
    
    void main(void){
    
    char data[128];
    float result;
    
    clrscr();
    gets(data);
    result=Convert(data);
    clrscr();
    printf("%f",result);
    getch();
    
    }
    
    float Convert(char data[128]){
    
    int i;
    float max,res,power,value;
    
    max=strlen(data);
    res=0.0;
    for (i=0;i<max;i++){
    	power=max-i-1;
    	value=power*atof(data[i]-'0');
    	res=res+value;
    }
    
    return res;
    }
    As I was saying power represents the 10^n value according to the decimal position, but for some reason atof(data[i]-'0') isn't working. Any ideas? Thank you.
    Don't Panic!

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    You were close on atof() . atof() will take a string (char *) and convert to a double. If I say: atof("1.503"), I get a double set to 1.503. (It's in stdlib.h as well.)

    You're feeding the function a plain old char... which could result in odd data/crashes. (Your compiler should've stopped you... but might've missed it since you didn't include the header.) Also, since it returns double, use double, they're more accurate than floats.

    Edit: If I'm reading this right, I think atof() does exactly what you want Convert() to do. Do read the warning about atof() not returning errors in that man page as well.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    10

    Eureka

    Cheers! I'm coding as we speak see if it works better.
    Don't Panic!

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    66
    atof converts a string to Float, you don't need to go character by character
    just put res=atof(data);

    I recommend first to check if all values are numbers or in case there is a point, that there's only one

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    10

    Eureka

    Cheers! Worked beautifuly.
    Don't Panic!

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    10

    Eureka

    Cheers! Worked beautifuly.
    Don't Panic!

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, 01: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, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21