Cin not failing for reading unsigned int

Hi all,

Got a really simple question. I feel I'm overlooking something.

1
2
3
4
5
6
7
8
	unsigned int tempNumber;

	cin >> tempNumber;

	while(cin.fail())
	{
		// Correction logic...
	}


If one enters a negative number with the above code, the fail/bad bits don't seem to be set (i.e. while loop is skipped), and tempNumber is set to the value of std::numeric_limits<unsigned int>::max() plus whatever negative value was entered.

Question: Is this normal behaviour, or am I missing something?

I'm using the VC++ compiler with VS2008 Express.
Tested using gcc 4.4 and it set the failbit.

Tested using VC++ 2010 and it did not set the failbit.

I don't know that this behavior is normal for VC++, but it probably is.
Thanks heaps for your response. That's weird behaviour, don't you think? Could one call that a compiler bug? Maybe it says something about this in the standard, I'll take a look.
Topic archived. No new replies allowed.