//  A small test to illustrate some looping properties
//  and handling of i/o streams
//  for CS 1160    Winter 2005	  W.R.Nico
//  Filename: loop-and-io-test.cpp

#include 
#include 

static char msg[]="@(#)loop-and-io-test.cpp	January 2005	W.R.Nico";

using namespace std;
int main(){
	int n = 100;
	char ch;
	string s;

	for(n=50; n < 52 ; n++){
	    cout << n << " ";
	}
        cout << " : after the loop n = " << n << endl;

	// You are not *required* to initialize in for:
	for(  ;n > 50; n--){
	    cout << n << " ";
	}
        cout << " : after the loop n = " << n << endl;

	// One can actually declare a new variable in the initialization
	// part.
	for(int n=1; n< 10; n++){
	    // The "interior" of a for loop is treated
	    // as a new "scope" according to the C++ standard.
	    // This means that the n declared here is *different*
	    // from the one declared above in main.
	    cout << n;
	}
	cout << endl;
	cout << n << endl; // This is the n in main.

	// If one uses cin to read numbers, e.g., ints
	// whitespace is skipped.
	cout << "Type some integers. Use '-1' to quit." << endl;
	cin >> n;   // "Prime" the while loop.
	while (n != -1){
	     // Check for bad input. If cin goes
	     // into an error state, it *stays* there.
          if(cin){ // Input OK
	   cout << "Got: " << n << endl;
          } else {
	   cout << "Bad input! Skipping to next line." << endl;
             // When cin gets into an error condition
             // one must call cin.clear() to 
             // recover from the error state.
	   cin.clear();
	     // Now we can call cin.ignore().
	   cin.ignore(512, '\n');

	 }
	   cin >> n;
	}

	// If one uses cin to read chars, whitespace
	// is skipped.
	cout << "Type some characters. Use 'Z' to quit" << endl;
	cin >> ch;    // "Prime" the while loop.
	while (ch != 'Z'){
	    cout <<"'" << ch <<"' ";
	    cin >> ch;
	}
	cout << endl << "Done getting characters" << endl;

	// At this point there is still a "newline" character
	// in the input stream.  In order to "flush" that
	// stream, do the following (assuming <= 100 chars are
	// left in it.  There may be extra characters if
	// the 'Z' was not the last character on the line.
	cin.ignore(100, '\n');

	// To get *all* characters, including whitespace,
	// use cin.get().
	// The '\' char in C++ means "take the next character literally".
	// It is also used for several special characters, e.g.,
	// \n is newline, \t is tab.

        cout << "Type some characters. The second '\\n' quits." <> s;
		cout << "Got \"" << s << "\""<