cout format problem

i want to print a list of format like the following:

NO WORD COUNT
1 abc 1
2 abc 7

but as no, word, and count is variable, i can't ensure how long it is.
how can i print the format like that? i used "\t" to cout. but the format is still not like that, can anybody tell me how to do that format??
If they are variable, you could store them in an std::string and use the size() member to see how long it is, then space the columns accordingly.
Hello if you are using the gcc-g++ compiler as in Linux then you could use the 'set field' option
from the header file iomanip(I/O manipluation). There are many other features in it you may use, but I
will show you the set field feature which I think will suit your program.

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

int main(){
//
//use a field width of 20
//
cout << "Right justification:" << endl;
cout << "|" << setw(20) << "your data here" << "|" << endl;
cout << "Left justification: " << endl;
cout << "|" << setw(20) << left << "your data here" << "|" << endl;
//
// you can set the format fields to suit your data as in...
//
cout << setw(10) << right << "This" << endl << setw(10) << "is" << endl << setw(10) <<
"a" << endl << setw(10) << "column" << endl << setw(10) << "of" << endl <<
setw(10) << "words" << endl;

cout << endl << "Or a simple right justified formatted table:" << endl;
cout << setw(12) << right << "ABC" << setw(12) << "DEF" << setw(12) << "GHI" << endl;
cout << setw(12) << "1ABC" << setw(12) << "1DEF" << setw(12) << "1GHI" << endl;
cout << setw(12) << "12ABC" << setw(12) << "12DEF" << setw(12) << "12GHI" << endl;
cout << setw(12) << "123ABC" << setw(12) << "123DEF" << setw(12) << "123GHI" << endl;

cout << "Finally the same output left justified:" << endl;
cout << "\t" << setw(12) << left << "ABC" << setw(12) << "DEF" << setw(12) << "GHI" << endl;
cout << "\t" << setw(12) << "1ABC" << setw(12) << "1DEF" << setw(12) << "1GHI" << endl;
cout << "\t" << setw(12) << "12ABC" << setw(12) << "12DEF" << setw(12) << "12GHI" << endl;
cout << "\t" << setw(12) << "123ABC" << setw(12) << "123DEF" << setw(12) << "123GHI" << endl;

// This is just a simple example of how iomanip can assist in foramtting data of all types.
return(0);
}
Topic archived. No new replies allowed.