/* unsigned.cpp -- How unsigned integers can give us surprises.
 *                 Given a string s, we print it out on successive lines
 *                 removing each time the leading character. We terminate 
 *                 when the length of the string becomes less than 3.
 */

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s = "abracadabra";

	while (s.length()-3 >= 0)
	{
	   cout << s << endl;
	   s.erase(0,1);
	}
	// This program results in an infinite loop.
	// The problem is that (s.length()-3 >= 0) 
	// is not the same as (s.length() >= 3).
	// s.length()-3 is an unsigned integer, so that when 1 is subtracted
	// from 0 we get a very big integer. The expression never goes negative.
}
