user1742554
user1742554

Reputation: 31

Structured output

I recently started programming in c++ and I've bumped into a small problem. If I want my output to be structured (let's say that every line starts with a name and then a number) in a way that the names are written normally to the screen (every first letter of every name starts at the beginning of each new line) and I want the numbers that follow to be lined up in a column, how would I do this? I want the programs output to look like this:

Gary         0
LongName     0
VerylongName 0

I want my program to print something in the way above, but with different lengths of names (and the '0' in this case, lined up in a column).

Upvotes: 1

Views: 396

Answers (4)

anorm
anorm

Reputation: 2263

Not in the C++ standard library, but still worth mentioning: boost::format. It will let you write printf-like format strings while still being type-safe.

Example:

#include <boost/format.hpp>
#include <iostream>
#include <string>

struct PersonData
{
    std::string name;
    int         age;
};

PersonData persons[] =
{
    {"Gary", 1},
    {"Whitney", 12},
    {"Josephine ", 101}
};

int main(void)
{
    for (auto person : persons)
    {
        std::cout << boost::format("%-20s %5i") % person.name % person.age << std::endl;
    }

    return 0;
}  

Outputs:

Gary                     1
Whitney                 12
Josephine              101

Upvotes: 1

Artur
Artur

Reputation: 7267

struct X
{
    const char *s;
    int num;
} tab[] = {
            {"Gary",1},
            {"LongName",23},
            {"VeryLongName",456}
          };

int main(void)
{
    for (int i = 0; i < sizeof(tab) / sizeof(struct X); i++ )
    {
        // C like - example width 20chars
        //printf( "%-20s %5i\n", tab[i].s, tab[i].num );
        // C++ like
        std::cout << std::setw(20) << std::left << tab[i].s << std::setw(5) << std::right << tab[i].num << std::endl;
    }

    getchar();
    return 0;
}  

Upvotes: 0

Tom
Tom

Reputation: 2399

Try the following: if you know the maximum length of all the names you intend to print (e.g. 20), then use the C++ i/o manipulators to set the width of the output (and left-justification). This will force the output to take up max characters.

Code snippet:

#include <iostream>
#include <iomanip>
...
// for each entry
  std::cout << std::setw(20) << std::left << "Gary" << 10 << "\n";
...
std::cout << std::flush;

Here's some more information...

Upvotes: 4

Dan L
Dan L

Reputation: 325

I'm shooting in the dark here since you haven't really included much information... HOWEVER one way you can do this is to make sure that you create the columns with padding around the name - and not worry about the numbers. Formatted output is one case where C has an advantage over C++ (IMHO). In C++ you can also do this with something like this:

cout << setw(15) << name << number << "\n";

Bonus points if you figure out ahead of time the maximum length of the name you have and add, say, 4 to it.

Upvotes: 1

Related Questions