/* Copyright: 1999, All rights reserved. Program: FormatedIO Version: 1.0 Created: 9/22/99 Revised: 10/20/99 File: formatedio.cpp Programmer: C. S. Tritt, Ph.D. Course: CS-150 Assignment: Example Compiler: Microsoft Visual C++ 6.0 Target: Win32 Description: This program tests C++ formated Input/Output operation. Revisions: There have been no major revisions of this program. Constants and Variables: See code for constant and variable descriptions. */ // Included files. #include // Required for stream I/O. #include // Required for formated I/O. #include // Required for strings. using namespace std; int main() { // Define some variables. double a = 12.34; double b = 123456.78901; double c = 1.200001; int n = 123; string text = "This is a long sentence."; string junk; // Try some unformatted and formatted output. cout << "The following lines are unformatted\n"; cout << "1234567890\n"; // Create a ruler. cout << a << "\n"; cout << b << "\n"; cout << c << "\n"; cout << n << "\n"; cout << text << "\n"; cout << "\n"; cout << "The following lines are width = 10.\n"; cout << "1234567890\n"; // Create a ruler. cout << setw(10) << a << "\n"; cout << setw(10) << b << "\n"; cout << setw(10) << c << "\n"; cout << setw(10) << n << "\n"; cout << setw(10) << text << "\n"; cout << "\n"; // Use a dummy read to prevent scrolling. cout << "Enter \"c\" to continue... "; cin >> junk; cout << "\n\n"; // Try some more formatted output. cout << "The following lines are width = 10 and precision = 4.\n"; cout << setprecision(4); // Setpercision(n) is good until changed. cout << "1234567890\n"; // Create a ruler. cout << setw(10) << a << "\n"; cout << setw(10) << b << "\n"; cout << setw(10) << c << "\n"; cout << setw(10) << n << "\n"; cout << setw(10) << text << "\n"; cout << "\n"; cout << "The following lines are width = 8, precision = 2 & fixed.\n"; cout << setprecision(2) << fixed; // Fixed is good until changed. cout << "1234567890\n"; // Create a ruler. cout << setw(8) << a << "\n"; cout << setw(8) << b << "\n"; cout << setw(8) << c << "\n"; cout << setw(8) << n << "\n"; cout << setw(8) << text << "\n"; cout << "\n"; // Everything worked. return 0; }