// ==========================================================================
// $Id: demo_stream.cpp,v 1.2 2009/10/21 21:48:48 jlang Exp $
// CSI2372 example Code for lecture 8
// ==========================================================================
// (C)opyright:
//
//   Jochen Lang
//   SITE, University of Ottawa
//   800 King Edward Ave.
//   Ottawa, On., K1N 6N5
//   Canada. 
//   http://www.site.uottawa.ca
// 
// Creator: jlang (Jochen Lang)
// Email:   jlang@site.uottawa.ca
// ==========================================================================
// $Log: demo_stream.cpp,v $
// Revision 1.2  2009/10/21 21:48:48  jlang
// More explicit output
//
// Revision 1.1  2006/10/17 22:18:48  jlang
// Added examples for lecture 8
//
//
// ==========================================================================
#include <iostream>

using namespace std;

int main() {
  int myInt = 100;
  float myFloat = 3.98764f;
  char myChar = 'x';
  char* myString = "Old style C-string.";
  cout << myInt << myChar << myFloat << endl;
  cout << myString << endl;
  // Definition of ios 
  // typedef basic_ios<char, char_traits<char> > ios;
  // Formatted output with manipulators
  cout << dec << myInt << ": ";
  cout << hex << myInt << " ";
  cout << oct << myInt << endl;
  dec( cout );
  // Some state manipulation
  cout.setstate( ios::badbit );
  int badB = cout.bad();
  cout.clear();
  cout << "Bad bit: " << badB << endl;
  // Put a character
  cout.put( myChar );
  cout.put(' ');
  // Put character directly in basic_streambuf
  cout.rdbuf()->sputc( myChar );
  cout << endl;

  // Read an integer
  cout << "Integer: ";
  cin >> myInt;
  if ( !cin.fail() ) {
    cout << myInt << endl;
  } else {
    cout << "Not an integer!" << endl;
    cout << "State: "; 
    if ( cin.rdstate() ) 
      cout << "clear!";
    else
      cout << "not clear!";
    cout << endl;
    cin.clear();
  }
  // Read but keep position the same 
  int curChar = cin.rdbuf()->sgetc();
  if ( curChar != '\n' ) {  
    cout << "Extra characters in cin!" << endl;
    cout << "Reading all of cin: " << endl;
    do {
      // Read and advance the position
      curChar = cin.rdbuf()->sbumpc(); 
      cout << static_cast<char>(curChar);
    } while ( curChar != '\n'  );
    cout << endl;
  }
}
