Using width member function: cout.width(), cin.width() in C++ programming
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info: none
To do: Using width member function: cout.width(), cin.width() in C++ programming
To show: How to use the width member function: cout.width(), cin.width() in C++ programming
// using width member function
#include <iostream>
using namespace std;
void main(void)
{
int p = 6;
char string[20];
cout<<"Using field width with setw() or width()"<<endl;
cout<<"----------------------------------------"<<endl;
cout<<"Enter a line of text:"<<endl>>string)
{
cout.width(p++);
cout<<string<<endl;
cin.width(7);
// For PC compatibles, use ctrl-z followed by return key or ctrl-d to exit
}
}
Output example:
Using field width with setw() or width()
----------------------------------------
Enter a line of text:
This is a line of test text from standard input
This
is
a
line
of
test
text
from
standa
rd
input
^Z
Press any key to continue . . .