user1203499
user1203499

Reputation: 267

Displaying tables in C++ console window

cout<<"  Name\t"
    <<"Cat\t"
    <<"Barcode\t"
    <<"Price\t"
    <<"Manufa\t"
    <<"Stock\t"
    <<"Sold\t"
    <<"ExDate\t "
    <<"Disc"<<endl;
for (unsigned int i=0; i < _storage.size(); i++)
{
    cout <<i <<":";
    _storage[i]->showData();
    cout<<endl;
}

I am trying to display data in a aligned manner.I am currently using the `t` character to do this but this will result in a misalignment if the data in one of the variable is too long.

How do I properly display data in a table form in C++?

Upvotes: 4

Views: 2746

Answers (2)

twain249
twain249

Reputation: 5706

you can use setfill and setw to set the fill char and width of the columns. The problem is you are going to have to limit the columns for it to look right.

Upvotes: 1

perreal
perreal

Reputation: 97948

You can use std::setw to set string width:

std::cout << std::setw (5) << "ASM" << std::endl;

So instead of using tabs, pad your string to a sufficiently large length.

Upvotes: 5

Related Questions