Converting Int Value to a string

This is a discussion on Converting Int Value to a string within the C++ Programming forums, part of the General Programming Boards category; Ok my code work, but only to a limit of 10 digits, can someone tell me why it wont work ...

  1. #1
    Wen Resu
    Join Date
    May 2003
    Posts
    219

    Converting Int Value to a string

    Ok my code work, but only to a limit of 10 digits, can someone tell me why it wont work after the 10th digit
    I'm making this to emulate a Delphi function call IntToStr.

    Code:
    int main(int argc, char *argv[])
    {
      int number;
      cout<<"Enter a number: ";
      cin>>number;
      cout<<endl;
      
      int x;
      int rema, count, digits;
      
      rema = 1;
      rema = number;
      digits = 0;
      while (rema != 0) {
      // calculate how many digits int has
        rema = rema / 10;
        digits++;
        }
        
      char numstr[digits];
      count = digits - 1;
      int left = number;
      
      while (count >= 0) {
      // stick the numbers into an array of char startign from then end so they will be in order
       numstr[count] =  (left % 10) + '0';
       left = left / 10;
       count--;
       }
      string answer = numstr;
      cout<<numstr<<endl<<answer<<endl; 
      
      
      system("PAUSE");	
      return 0;
    }
    Last edited by Iamien; 05-24-2003 at 06:41 PM.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Most likely you're running over the maximum allowed integer for an int, and have some rap-around problems going on. The logic of your algorithm itself seems to be just fine.

    Note: The maximum positive value for a signed 32-bit integer is 2,147,483,647 - a 10-digit number.

    Cheers
    Last edited by Zach L.; 05-24-2003 at 06:57 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    thanks. wish it could do better then 10 digits, guess i'll live with it though;

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    No problem. Depending on your compiler, you might have an __int64 type. An unsigned __int64 will give you at least 18 digits.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    thanks, was thinking about thast. reaidng compiler help files atm
    far as i can tell int shouldn't be able to hold anything outside
    -32768 to 32767. i shall read on!

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Try using a long instead.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    same problem.. perhaps somethign wrong within algorythm

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    On most PCs and such, a short is 16 bits (with range -32768 - 32727), a long is 32 bits (with that ten digit number which happens to be 2^31 - 1 as its range), and an int is one of those two. If you make an integer unsigned, then it can't have any negative values, but twice (plus one) as many positive values.

    cout << sizeof(int); will tell you exactly how many bytes you have in an int.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    int is 4 bytes. i'm doing some recoding to let it us unsigned if its negative. well the number pass if negative is made psoitive but that fact it was negative is stores and added to start of string in the end

  10. #10
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    Hmm unsigned still not helping, can't go over 10. must be something in my code.

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    No... the max unsigned int is still a ten digit number. Your algorithm looks just fine. Try an unsigned __int64. If you're programming in Windows, chances are your compiler will have it.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #12
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    ah thanks. i was still using int.

  13. #13
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    Woohoo it works... now i was stupid when i started this... i'm used to delphi where you cannot output a interger in a string based output, but so far in C++ i havn't had a problem output an int with cout well at least i have this if i need it.

  14. #14
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    can't you just use itoa?

    char *buffer[255];

    itoa(buffer, 255, myint);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  3. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM

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