ex8-2.cpp - Example 8-2 ios_base member functions

// File: ex8-2.cpp - ios_base member functions

#include "fmtflags.h"

int main() 
{
	// Display cout's default width, fill, and precision settings
	cout << "cout.width()=" << cout.width() <<endl;
	cout << "cout.fill()=" << cout.fill() << ' ' << (int) cout.fill() << endl;
	cout << "cout.precision()=" << cout.precision() <<endl;

	// Display cin's default width, fill, and precision settings
	cout << "cin.width()=" << cin.width() <<endl;
	cout << "cin.fill()=" << cin.fill() << ' ' << (int) cin.fill() << endl;
	cout << "cin.precision()=" << cin.precision() <<endl;

	// Display cerr's default width, fill, and precision settings
	cout << "cerr.width()=" << cerr.width() <<endl;
	cout << "cerr.fill()=" << cerr.fill() << ' ' << (int) cerr.fill() << endl;
	cout << "cerr.precision()=" << cerr.precision() <<endl;

	// Demonstrate the ios_base::width() function
	cout << 1 << 2 << 3 << endl;
	cout.width(5);
	cout << 1 << 2 << 3 << endl;

	// Demonstrate the width() function for char data
	cout.width(5);
	cout << 'a' << 'b' << 'c' << endl;
	cout.width(5);
	cout << "a" << "b" << "c" << endl;

	// What happens when a value's length exceeds the width setting
	cout.width(3);
	cout << 123456789 << '|' <<"hey\n";

	// width and left justification
	cout.setf(ios::left,ios::adjustfield);
	cout.width(5);
	cout << 1 << 2 << 3 << endl;

	// Demonstrate fill()
	cout.fill('$');	
	cout.width(10);
	cout << 1 << '|' << 1 << endl;

	// Set precision to 4
	cout.precision(4);
	cout.width(10);
	cout << 123.45678 << '|' << 1234567.8 << '|' << 1.2345678 << '|' << 12345678 << endl;

	// precision set to 4 and fixed flag set
	cout.setf(ios::fixed,ios::floatfield);
	cout.width(10);
	cout << 123.45678 << '|' << 1234567.8 << '|' << 1.2345678 << '|' << 12345678 << endl;

	// Any difference between float and double?
	float f = 314.f;
	cout << f << '|' << 314. << endl;

	// Turn off fixed setting
	cout.unsetf(ios::fixed);
	cout << f << '|' << 314. << endl;

	// Turn on showpoint
	cout.setf(ios::showpoint);
	cout << f << '|' << 314. << endl;

	// Turn on fixed
	cout.setf(ios::fixed,ios::floatfield);
	cout << 123.45678 << '|' << 1234567.8 << '|' << 1.2345678 << '|' << 12345678 << endl;

	// Clear the flags for cout and turn on hex for cin
	cout.flags(ios_base::fmtflags(0));
	cin.setf(ios::hex|ios::basefield);
	cout.width(35);
	cout << "Enter a hexadecimal number => ";
	int Hex;
	cin >> Hex;

	// Display the Hex value in decimal and hex
	cout << "Hex=" << Hex << '|' << hex << Hex << endl;

	// Check the format flags for cin and cout
	show_fmtflags(cin);
	show_fmtflags(cout);

	// Turn on hex, the right way
	cin.setf(ios::hex,ios::basefield);
	cout << "Try again, dummy => ";
	cin >> Hex;
	cout << Hex << endl;

	// Is there a problem with cin
	cout << "cin.rdstate=" << cin.rdstate() << endl;

	// What are the format flag settings for cin and cout
	show_fmtflags(cin);
	show_fmtflags(cout);

	// Fix the problem with cin
	cin.clear();
	cout << "cin.rdstate=" << cin.rdstate() << endl;

	// Clear the input buffer
	cin.ignore(50,'\n');

	// Try again for the Hex input
	cout << "Ok, get it right this time => ";
	cin >> Hex;
	cout << Hex << endl;

	return 0;
}



CIS27: Programming in C++    Instructor: Joe Bentley