Lining Up Text Output with setw function - C++ File Stream

C++ examples for File Stream:cout

Description

Lining Up Text Output with setw function

Demo Code

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main() {// w  w  w. j  a v  a2 s  . c o m

   ios_base::fmtflags flags = cout.flags();
   string first, last, citystate;
   int width = 20;

   first = "first";
   last  = "last";
   citystate = "New York, AZ";

   cout << left                      // Left-justify in each field
        << setw(width) << first      // Then, repeatedly set the width
        << setw(width) << last       // and write some data
        << setw(width) << citystate << endl;

   cout.flags(flags);
}

Result


Related Tutorials