Capturing an "integer" with too many digits

I'm working on a program that handles "integers" that are too long to be legal integers in C++. I've got most of it handled, but in testing, I discovered that if I get an input as a numerical value where the number of digits is exceptionally long, the compiler uses the typical maximum or minimum value of an integer (32,767) and I get an unrecognizable number. This is exactly what the program is meant to handle.

For example, say I get the number 1000000945831 (totally made up for testing). If I get that number passed as a string, I'm fine. My program can handle it. But if that number gets passed as an number, things break down.

What I was trying to do (successfully with smaller numbers), was to use a stringstream like this:

1
2
3
4
5
6
7
<class>::<class>(int myValue)  //constructor

stringstream ss;
string strMyValue

ss << myValue
strMyValue = ss.str();


As I said, this works fine with smaller numbers, but the whole point is to handle very large numbers. If someone were to call this like:

1
2
3
int main()

<class> <object>(1000000945831)


the number that enters the stringstream has been converted due the limitations with integer handling. Is there another way to handle this so that I don't lose the initial value? I'm a little stumped.

Thanks.
Have them pass a string representing the integer, then parse it to whatever format you store it in.
If it were only that easy... This is a class exercise, so I'm trying to code to handle all situations. As I mentioned, when it is passed as a string, I'm fine. I'm trying to figure out a way to handle the integer specifically when called in the way that I described above.
If you pass a large number as an integer, it will overflow. I'd do what firedraco suggests, handle it like a string in the first place. I don't really see why you wouldn't, as far as I can tell if you try to store such a large number in an int, it will overflow.
Topic archived. No new replies allowed.