# Thread: another question-sticking one number onto another

1. ## 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. 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)

3. 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.

4. 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. 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.