Set cout length : cout width « Console « C++






Set cout length

Set cout length
 
#include <iostream>
#include <cstring>
using namespace std;

void center(char *s);

int main()
{
  center("www.java2s.com!");
  center("www.java2s.com");

  return 0;
}

void center(char *s)
{
  int len;

  len = 40+(strlen(s)/2);

  cout.width(len);
  cout << s << '\n';
}



           
         
  








Related examples in the same category

1.Create a table of log10 and log from 2 through 100.Create a table of log10 and log from 2 through 100.
2.Left-justification and right-justification.
3.cout: width(), precision(), and fill(). cout: width(), precision(), and fill().