Thread: Unable to get something besides an Integer returned

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    18

    Unable to get something besides an Integer returned

    I am working on a program that can concern a string into a number, such as 245.5. Unfortunately, I am unable to render the decimal part of the number.

    Ive delcared my function and vairables as floats. The algorithm needs a little work, but I am specifically asking about getting a decimel value.

    Code:
    #include <stdio.h>
    
    
    float strToInt (const char string[])
    {
        int i, negative, test;
        float result = 0, remainder, intValue;
        negative = 0;
        remainder = 0;
        test = 0;
    
    
    if (string[0] == '-'){
                    negative = 1;
                    }
    i = negative;
        while ((string[i] >= '0' && string[i] <= '9') || string[i] == '.')
        {
            if (string[i] == '.' || test == 1){
            test = 1;
            remainder = (string[i+1]/100);
            printf ("%f\n", remainder);
            ++i;
            }
            if (test == 0){
            intValue = string[i] - '0';
            result = result*10 + intValue;
            ++i;
            }
    
    
        }
        if (negative == 1){
            result = result * -1;
        }
    
    
        return result;
    }
    
    
    
    
    int main (void)
    {
    float strToInt (const char string[]);
    
    
        printf ("%f\n", strToInt("245.5"));
        printf ("%f\n", strToInt ("100")+ 25);
        printf ("%f\n", strToInt("13x5"));
    
    
        return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The most immediate problem is you're using integer division, which removes any precision in the result.
    Code:
    string[i+1]/100
    should be
    Code:
    string[i+1]/100.0
    By converting either operand to floating point, the entire expression is evaluated as floating point and you preserve any precision. However, you'll find that this algorithm doesn't work correctly. I'm torn between telling you why and letting you figure it out on your own though.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    120
    To convert a string to float I suppose you could do something like this:
    Value starts at 0,
    Number is the decimal number represented by the character to be read.
    Before the comma/point:
    multiply value by 10, add number.
    After the comma/point:
    add number * (.1 ^ (number position - point/comma position))

    I think this should work. Keep in mind results may vary because of precision issues.

    I highly recomend you do not convert floats in a function called strToInt, make a separate function for that. Also, in a function that is supposed to return an int, dont store the value to return in a float or you may have precision problems which will mean the convertion might not be exact (and the point with integers is to have precision so...).

    Also, fix your formating for god's sake, your code reeks of school.

    I also have no idea what you're trying to do with the variable "remainder", but that might be because the code is horribly formated, which means my ADD can't keep up.

  4. #4
    Registered User
    Join Date
    Mar 2015
    Posts
    18
    Quote Originally Posted by Prelude View Post
    The most immediate problem is you're using integer division, which removes any precision in the result.
    Code:
    string[i+1]/100
    should be
    Code:
    string[i+1]/100.0
    By converting either operand to floating point, the entire expression is evaluated as floating point and you preserve any precision. However, you'll find that this algorithm doesn't work correctly. I'm torn between telling you why and letting you figure it out on your own though.
    Yea adding the .0 at the end there has got me on my way.

    Dont bother with fixing the algorithm, i knew the problem before (and mentioned that it needed work to be functional) but im too lousy at this to assume somethign will work without testing it constantly, which is why i wanted to stop there and ask about these floats

  5. #5
    Registered User
    Join Date
    Mar 2015
    Posts
    18
    Looks like school eh? I'll take that as a compliment, as Im currently teaching myself. Remainder is for the decimel values. Thank you for your input, ill try to be neater in formatting in the future.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    120
    Quote Originally Posted by JerrySingh View Post
    I'll take that as a compliment
    It wasn't. If you're teaching yourself I'm sorry I said that, and keep at it. If you can do it it's the smartest way to learn.

    If you want to return a separate remaider to the integer value you could have the user provide a pointer which you could load with the value of the remainder (or ignore, if NULL was passed), but if your function implies you give the user an int, you should really return an int and not a float.

    As for the formating issues, it really shouldn't be that hard with a decent text editor. Just use something basic like notepad++ for windows, which remembers the indentation of the previous line, and make sure you remember to go back and forth when you close/open brackets. If you want something that handles indentation automagically you can also look at some more advanced text editors, like emacs or vim, but those take time to learn how to use them decently. There are probably simpler editors that do automatic indentation, but I don't know many editors at all, so I can't help much there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What happens behind the scene when value is returned ?
    By afesheir in forum C Programming
    Replies: 6
    Last Post: 12-01-2011, 11:41 AM
  2. *** process returned -1 ***
    By bilbo67216 in forum C Programming
    Replies: 16
    Last Post: 05-28-2010, 12:35 AM
  3. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  4. What exactly is iterator returned by .end()?
    By pheres in forum C++ Programming
    Replies: 21
    Last Post: 12-09-2008, 09:50 AM
  5. I've Returned
    By Krak in forum Game Programming
    Replies: 11
    Last Post: 05-06-2003, 11:53 AM

Tags for this Thread