Thread: itoa trouble

  1. #1

    itoa trouble

    I don't have an itoa() function for my compiler. So i decided to write one. The code folloes, but it has an odd bug. The func works perfectly until it exits the for loop, and it somehow "forgets" everything it has stored in my num array. Please help!

    Code:
    char *itoa(int i, char num[256]){
      int rem = 0;
      int digits = 1;
      int x=i;
    
      for (digits; true; digits++){
        if (x<10){
          cout << "Digits: " << digits << endl;
          break;
        } else {
          rem = x % 10;
          x = (x-rem)/10;
          //cout << n << ": i:" << i << endl;
        }
      }
      rem=0;
      for (int n=digits; n>=0; n--){
        if (i<10){
          num[n]=(char)(i+48);
          cout << n << ": n:" << num[n] << " :" << (int)num[n] << endl;
          cout << "    " << num[n] << endl;
          break;
        } else {
          rem = i % 10;
          num[n]=(char)(rem+48);
          cout << n << ": n:" << num[n] << " :" << (int)num[n] << endl;
          cout << n << ": R:" << rem << endl;
          i = (i-rem)/10;
          cout << n << ": i:" << i << endl;
        }
        cout << "    " << num[n] << endl;
    
      }
      num[digits]='\0';
      cout << "Number: " << (int)num[0] << endl;
      if(getche());
      return num;
    }
    All help will be appreciated.
    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Try and change char num[256] into char* num. You need a pointer, otherwise num will go out of scope when exiting the function and you can't access the data anymore.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    use sprintf ("%d", var)
    hello, internet!

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    OT

    Seems like it's very popular with those "anti coding style" avatars .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    never knew that! thanks!

    and the couts are just for debugging... so i can't use printf()

    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  6. #6
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Inquirer
    never knew that! thanks!

    and the couts are just for debugging... so i can't use printf()

    ~Inquirer
    not like that sentence made sense (cout goes to the same place as printf), but i said use sprintf not printf . and if you're using cout why not just

    int i;
    ...
    cout << i;

    ??
    hello, internet!

  7. #7
    The couts are just for debuging, and to prove to me that the loop was doing what it was supposed to. Using sprintf() wouldn't (i don't think ) help me convert a number into a string to bre sent through a socket. What is sorintf anyway? String print f?

    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    sprintf() won't print to the screen, unlike printf() (notice the s in front). Instead it will print to a buffer (char array) so you can do whatever you want with the string.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    How would I implement that?

    And does anyone know why the array goes out of scope, or ow to circumvent that? Ir will sprintf not make it go out of scope?

    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Using sprintf() wouldn't (i don't think ) help me convert a number into a string to bre sent through a socket.
    The end result is the same as the non-standard itoa, a string with the numeric value of an integer.

    >What is sorintf anyway?
    You don't know what it is yet you say that it won't work for your purposes?
    Code:
    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    
    const int DIGITS = 10;
    
    char *itoa ( int n )
    {
      int sign;
    
      if ( ( sign = n ) < 0 )
        n = -n;
    
      int i = 0;
      char *s = new char[DIGITS];
    
      do
        s[i++] = static_cast<char> ( n % 10 + '0' );
      while ( ( n /= 10 ) > 0 );
    
      if ( sign < 0 )
        s[i++] = '-';
      s[i] = '\0';
    
      std::reverse ( s, s + std::strlen ( s ) );
    
      return s;
    }
    
    int main()
    {
      int i = 12345;
    
      char *p = itoa ( i );
      std::cout<< p <<std::endl;
      delete p;
    
      char a[10];
      std::sprintf ( a, "%d", i );
      std::cout<< a <<std::endl;
    
      std::cin.get();
    }
    -Prelude
    My best code is written with the delete key.

  11. #11
    Aparently, my idea of what sprintf was is wrong... thanks Prelude. I'm going to go step through that code to try to fugure out what it does, too. Thanks all.

    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with itoa() perhaps?
    By TheSquid in forum C++ Programming
    Replies: 5
    Last Post: 05-08-2006, 02:04 AM
  2. Really Weird itoa Problem
    By Grantyt3 in forum C++ Programming
    Replies: 8
    Last Post: 12-20-2005, 12:44 AM
  3. itoa function trouble ??
    By gemini_shooter in forum C Programming
    Replies: 4
    Last Post: 03-17-2005, 12:08 PM
  4. itoa
    By coldcoyote in forum Linux Programming
    Replies: 4
    Last Post: 02-13-2003, 09:28 AM
  5. itoa
    By Thantos in forum C Programming
    Replies: 2
    Last Post: 09-18-2001, 02:23 PM