Thread: problems with float-type functions

  1. #1
    Registered User
    Join Date
    Dec 2012
    Fukushima-shi, Fukushima, Japan, Japan

    problems with float-type functions

    this is a homework so were not supposed to use some commands which might make this hell of a lot easier. the users enters a decimal number and the programs stores it as an numerical string. im supposed to make it return the numerical string in float, hence, declaring the function a float. here's my code:

    float toFloat(void)
        float num = 0, dec = 0, temphold, ans;
        double multiplier = 1;
        char s[100];
        int x = 0, counter = 0, stopper = 0;
        printf("string? ");
        scanf("%s", &s);
        printf("\nYour string: %s", s);
        for(x = 0;s[x] != '\0';x++)
            if(s[x] == '.')
                printf("\ndecimal piot detected");
                for(x = x ; s[x] != '\0' ; x++)
                    multiplier = multiplier / 10;
                    temphold = s[x] - '0';
                    dec = dec + (temphold * multiplier);
                    printf("\nx: %c", s[x]);
                    printf("\ntemphold: %f", temphold);
                    printf("\nmultiplier: %f", multiplier);
                    printf("\ndec: %f", dec);
                    stopper = 1;
            else if(stopper == 0)
                printf("\ncounter: %d", counter);
        x = counter - 1;
        multiplier = 1;
        printf("\nx1: %d", x);
        while(counter > 0)
            temphold = (s[x] - '0');
            printf("\ntemphold: %f", temphold);
            num = num + (temphold * multiplier);
            printf("\nmulti: %f", multiplier);
            printf("\nx: %c", s[x]);
            printf("\nnum: %f", num);
            multiplier = multiplier * 10;
        ans = num + dec;
        printf("\n\n%g", ans);
        return ans;
    my main problem is i cant seem to make it return the float value as it should. eg. input = 123.45, it should return 123.45, not 112347... another problem is that there are random really small numbers (eg. 0.00002) suddenly appearing, but i dont really mind those things. i used %g so that those numbers wont show up. im still a first year computer science student so there might be things that i dont know or stupid things i put in my code so dont be too harsh on me

    you may also notice a lot of printfs there, im using it to trace my code, just to be sure.


    ps. one of my friends told me that its impossible to return exactly 123.45, but he's also not sure
    Last edited by cipher1285; 12-03-2012 at 08:30 PM. Reason: ps

  2. #2
    Registered User
    Join Date
    May 2009
    I suggest using or writing your own atoi function.

    atoi - C++ Reference

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Apr 2006
    Works for me. All I did was hardcode the string to run it online.

    There are a number of style issues, like poor variable names and awkward program flow, but the function seems to be correct.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float type- beginner Q
    By baxy in forum C Programming
    Replies: 2
    Last Post: 11-22-2012, 08:14 AM
  2. function type void or float
    By a.mlw.walker in forum C Programming
    Replies: 2
    Last Post: 08-23-2011, 04:09 AM
  3. Data type problems, float, int, etc with averages
    By Jomoka in forum C Programming
    Replies: 4
    Last Post: 06-18-2011, 08:18 AM
  4. Float Data Type Ranges
    By GokhanK in forum C Programming
    Replies: 1
    Last Post: 02-26-2011, 03:17 PM
  5. Float data type issue
    By Figen in forum C Programming
    Replies: 2
    Last Post: 01-31-2011, 01:42 PM

Tags for this Thread