[ACCEPTED]-Align cout format as table's columns-format

Accepted answer
Score: 55

setw.

#include <iostream>
#include <iomanip>
using namespace std;

int main () {
  cout << setw(21) << left << "Test"    << 1 << endl;
  cout << setw(21) << left << "Test2"   << 2 << endl;
  cout << setw(21) << left << "Iamlongverylongblah"     << 2 << endl;
  cout << setw(21) << left << "Etc"     << 1 << endl;
  return 0;
}

0

Score: 10

I advise using Boost Format. Use something like this:

cout << format("%|1$30| %2%") % var1 % var2;

0

Score: 2

You must find the length of the longest 6 string in the first column. Then you need 5 to output each string in the first column 4 in a field with the length being that of 3 that longest string. This necessarily means 2 you can't write anything until you've read 1 each and every string.

More Related questions