Thread: Incomprehensible Infinite Loop

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    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.
    Last edited by cunnus88; 02-08-2006 at 02:42 AM.

  2. #2
    Registered User Kurisu's Avatar
    Join Date
    Feb 2006
    Posts
    62
    Don't worry its not your function it is cin
    I thought there was a way to validate if what the user inputed was a valid integer datatype and if it wasn't you could just prompt again, but I can't remember.

    I dunno. Maybe try flushing cin down the toilet. [This isn't the technical definition of whats really going on, but by golly it should be!]
    Code:
    cin.clear();
    Code:
    while(true)
    {
    	cin >> input;
    cin.clear(); // Give that sucker a nice swirly right here. Note last valid INPUT will be processes TWICE as the junk INPUT.. say 12397329478729348782 will be gone.
    	count = 0;
    	s = factorialize(input);
    	cout << s.c_str() << endl;
    
    }
    Last edited by Kurisu; 02-08-2006 at 03:04 AM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The result of reading an integer is implementation defined if the actual input corresponds to a larger value than the integer can store, and a stream will typically be in an error state afterwards. Which (roughly) means the result is compiler and library specific.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    cin >> var; has all the same problems that scanf() has in C
    - No overflow detection
    - Poor error recovery
    - leaves unused characters (eg newlines) to foul up other input methods.

    If you really want to be sure of what you're getting, you have to read all user input as a string, and then validate it yourself.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Thanks guys. I guess it should have been obvious all along.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  4. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM
  5. question ab an infinite loop...
    By JohnMayer in forum C Programming
    Replies: 10
    Last Post: 07-26-2002, 10:15 AM