another question-sticking one number onto another

This is a discussion on another question-sticking one number onto another within the C++ Programming forums, part of the General Programming Boards category; I made this function which is supposed to append integer1 onto the end of integer 2. I'm sure this is ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    28

    another question-sticking one number onto another

    I made this function which is supposed to append integer1 onto the end of integer 2.
    I'm sure this is 5x more complicated than it needs to be, but right now optimization is not a priority.

    Code:
    long combine (long integer1, long integer2)
    {
        long integer3;
               // first converts the integers into strings
        stringstream ss1;
        stringstream ss2;
        ss1 << integer1;
        ss2 << integer2;
        string string1(ss1.str());
        string string2(ss2.str());
              // then appends string2 onto string1
        string1.append(string2);
              // converts string1 into an array, one character at a time so that atoi can be used
        int x=0;
        char array1[string1.length()];
        start:
        array1[x] = string1[x];
        x++;
        if (x != string1.length())
           goto start;
              // uses atoi to convert it back into an integer
        integer3 = atoi(array1);
        return integer3;
    }
    The problen is that it does work, but if one of the input numbers is too big (around 7 or 8 digits), it returns a crazy negative number, and I cant find out what is causing it.

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    There's a good chance that if one of the numbers is 7 or 8 digits, then both combined will not fit into a long. (On my system, long is 4 bytes, meaning no more than 10 digits). With that in mind, you can still simplify your code:
    Code:
    #include <iostream>
    #include <sstream>
    
    int main()
    {
      long num1=1234;
      long num2=1234;
      std::stringstream strm;
      strm<<num1<<num2;
      long num3;
      strm>>num3;
      std::cout<<num3;
    }
    If you need to operate on larger numbers, you might consider using a library like gmp (or writing your own large integer routines). The only other option I can think of is using something non-portable like a long long (if your compiler supports it)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Right. When a number is stored in binary, the leftmost digit represents the sign. If you write an "infinite loop" that simply counts-up (with a signed type), the count will suddenly turn negative! Try it... It's fun!

    You can use <climits> to check the maximum sizes on your system. You can also try using an unsigned long.
    Last edited by DougDbug; 07-18-2006 at 02:46 PM.

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I recommend that if you do the experiment that DougDbug is suggesting, increment by like 10,000 at first, since it can take a long time to max out.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    28
    As it turns out the problem is that the long was too big like you said. I did manage to find another way around it, so my program can still work, though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. help with number menu
    By j4k50n in forum C Programming
    Replies: 12
    Last Post: 04-17-2007, 01:33 AM
  5. Question about limiting the number of user inputs in C
    By chobibo in forum C Programming
    Replies: 15
    Last Post: 08-30-2006, 12:37 AM

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