Thread: function gives back the same value

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    23

    function gives back the same value

    hi,

    i wrote a small function that expects a double and gives back
    a char pointer with the value as char-string.
    the first time i use it in my program it works fine, but if i call the
    function a second or third time, even if i just cout the results both times,
    the results are always the same as the result of the first call.
    where is my fault?

    thanks,

    toby

    Code:
    char *doubletostring(double number) {
            char numberstring[18];
            int i=0;
            double divisor=100000000000000000;
            while (i<18) {
            if (number < divisor) {
            divisor=divisor/10;
            numberstring[i]=(int)32;
            i++;
            } else {
            numberstring[i]=(int)(48+(number/divisor));
            number=fmod(number,divisor);
            divisor=divisor/10;
            i++;
            }
            }
            numberstring[i]='\0';
            string cut0s;
            i=0;
            while (numberstring[i]==(int)32) {
              i++;
            }
            while (numberstring[i]!='\0') {
              cut0s += numberstring[i];
              i++;
            }
              cut0s += '\0';
            char *returnchars =(char*)cut0s.c_str();
            return returnchars;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are returning a pointer to a local variable, this is not valid - the variable's memory is "gone" by the time you return from the function. Either pass in (by reference) a string to store the text in, or allocate memory in the function (I would propose the former).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    matsp is correct.

    I would further add, however, if the block of memory will be static, or does not need to be resized, then have the caller pass it in, along with the size of the block of memory. If you need the memory to be dynamically sized with no way for the caller to have any idea of how big the buffer needs to be, then returning a pointer to dynamic memory is possibly a better solution. There are arguments for both approaches.

    Using std::string would be the best way, however, since the dynamically sized buffer is handled for you automatically.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What the heck is that mess? That looks like tomato juice to me.
    There's really a reason why we indent code - to make it readable! You should try it, too.
    Indent each block one tab and don't mix spaces and tabs.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I made a few minor changes to make it use std::string consistently and saw that you have a long way to go:

    Code:
    #include <string>
    #include <iostream>
    #include <cmath>
    using std::string;
    
    string doubletostring(double number) {
        string numberstring;
        std::size_t i=0; //more proper to use for comparisons against string::size
        double divisor=100000000000000000.0; //too large for a long
        while (i<18) {
            if (number < divisor) {
                divisor=divisor/10;
                numberstring += ' '; //why use 32 and why cast int to int?
                i++;
            } else {
                numberstring +=(char)('0'+(number/divisor));
                number=fmod(number,divisor);
                divisor=divisor/10;
                i++;
            }
        }
        //numberstring[i]='\0';
        string cut0s;
        i=0;
        while (i != numberstring.size() && numberstring[i]==' ') {
            i++;
        }
        while (i != numberstring.size()) {
            cut0s += numberstring[i];
            i++;
        }
        //cut0s += '\0';
        
        return cut0s;
    }
    
    int main()
    {
        double d;
        while (std::cin >> d) {
            std::cout << "As string: " << doubletostring(d) << '\n';
        }
    }
    A sample run:
    45
    As string: 45
    23.32
    As string: 23
    23.88
    As string: 23
    -1.1
    As string:
    1001
    As string: 1 1
    Apparently your code doesn't handle yet:
    - negative values,
    - decimal places,
    - zeros in numbers (why would you convert them to spaces?!),
    - and not shown here: very large values

    Unless this is a school exercise and you are required to do this this way, you might want to take a look into stringstreams.

    This functionality is also available in boost: lexical_cast

    Edit: if you mean character constants like ' ' or '0', don't obscure your code with magic numbers 32 and 48.
    Last edited by anon; 12-19-2007 at 02:38 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM