Incomprehensible Infinite Loop
As far as I understand it, the operator >> is capable of accepting integer arguments when used with the cin stream.
The problem is, when I go crazy on the keyboard and type in a group of numerical characters which far exceeds the size of an integer (on an intel based computer with VC++ 2003) such as 1872634817236491872364817, the value that gets passed to my integer is always (and I mean always) 3435973836.
That is certainly a head scratcher but I have this stream nested inside a while loop and once it receives my crazy input, it never stops at cin and continues going round and round and always passes 3435973836 to my integer.
Here's my code:
Code:
while(true)
{
cin >> input; // input is an integer. Feel free to type in
//any very large number at the prompt. Just make
//sure it exceeds 2^32. Preferrably much larger.
s = factorialize(input); // s was declared as a string elsewhere.
cout << s.c_str() << endl;
}
On the off chance that my function "factorialize" is causing the damage, here's the relevant function:
Code:
string factorialize(unsigned int input)
{
bool not_end = false;
unsigned int max = static_cast<int>(sqrt(static_cast<double>(input)));
string s;
for(unsigned int i = 2; i <= max; i++)
{
if(input % i == 0)
{
char buf[10];
itoa(i, buf, 10);
s += buf;
s += "x";
s += factorialize(input/i);
not_end = true;
break;
}
}
if(!not_end)
{
char buf[10];
itoa(input, buf, 10);
s = buf;
}
return s;
}
What in the world is going on here?
As always, I humbly await your responses.