problems with float-type functions

This is a discussion on problems with float-type functions within the C Programming forums, part of the General Programming Boards category; this is a homework so were not supposed to use some commands which might make this hell of a lot ...

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

    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:

    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");
                x++;
                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)
            {
                counter++;
                printf("\ncounter: %d", counter);
            }
        }
        printf("\n\nDIVISION\n\n");
        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;
            x--;
            counter--;
        }
    
    
        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.

    thanks

    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 07:30 PM. Reason: ps

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

    atoi - C++ Reference

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    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, 07: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, 02:17 PM
  5. Float data type issue
    By Figen in forum C Programming
    Replies: 2
    Last Post: 01-31-2011, 12:42 PM

Tags for this Thread


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