Reputation: 313
This is the expected output:
COUNT | WORD
------+------
1 | .3
1 | .3.4
1 | 3
2 | 12.34
1 | test1.12.34
3 | this
This is my proper code:
std::cout << "COUNT | WORD" << '\n';
std::cout << "------+------" << '\n';
std::cout << std::setw(4) << "1" << std::setw(3) << '|' << std::setw(3) << ".3" << '\n';
std::cout << std::setw(4) << "1" << std::setw(3) << '|' << std::setw(3) << ".3.4" << '\n';
std::cout << std::setw(4) << "1" << std::setw(3) << '|' << std::setw(3) << "3" << '\n';
std::cout << std::setw(4) << "2" << std::setw(3) << '|' << std::setw(3) << "12.34" << '\n';
std::cout << std::setw(4) << "1" << std::setw(3) << '|' << std::setw(3) << "test1.12.34" << '\n';
std::cout << std::setw(4) << "3" << std::setw(3) << '|' << std::setw(3) << "this" << '\n';
Unfortunately, my ouput's messy the WORD
COUNT | WORD
------+------
1 | .3
1 |.3.4
1 | 3
2 |12.34
1 |test1.12.34
2 |this
Could anyone suggest me a solution for that. Thanks
Upvotes: 1
Views: 2273
Reputation: 4148
Doing this will set the left hand side filler character.
cout.fill('-');
cout.width(40);
cout<< "LINE1" <<endl;
cout.fill('-');
cout.width(40);
cout<< 3 <<endl;
cout.fill('-');
cout.width(40);
cout<< 3.4 <<endl;
cout.fill('-');
cout.width(40);
cout<< "TEST " << 12.34 <<endl;
Upvotes: 0
Reputation: 6052
Why not this ::
std::cout << "COUNT | WORD" << '\n';
std::cout << "------+------" << '\n';
std::cout << std::setw(4) << "1" << std::setw(3) << '|' << ' ' << ".3" << '\n';
std::cout << std::setw(4) << "1" << std::setw(3) << '|' << ' ' << ".3.4" << '\n';
std::cout << std::setw(4) << "1" << std::setw(3) << '|' << ' ' << "3" << '\n';
std::cout << std::setw(4) << "2" << std::setw(3) << '|' << ' ' << "12.34" << '\n';
std::cout << std::setw(4) << "1" << std::setw(3) << '|' << ' ' << "test1.12.34" << '\n';
std::cout << std::setw(4) << "3" << std::setw(3) << '|' << ' ' << "this" << '\n';
Upvotes: 2
Reputation: 4739
Instead of having
std::cout << std::setw(4) << "1" << std::setw(3) << '|' << std::setw(3) << ".3" << '\n';
For each line, add a space after the '|' character:
std::cout << std::setw(4) << "1" << std::setw(3) << "| " << std::setw(3) << ".3" << '\n';
Upvotes: 2