Home > database >  Cpp/C Output allingment in one line from right AND left
Cpp/C Output allingment in one line from right AND left

Time:11-30

I need to write ints from the right and strings from the left into a single line and have them line up properly (view output below the code).

Basically I just need a way to write a table only using iostream and iomanip and change the allingment from right for ints to left for strings and back.

Other tips are also appreciated :)

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

class foo
{
public:
    int i;
    std::string s;
    int j;
    foo(int i1,std::string s1,int j1) : i(i1), s(s1),j(j1) {};
};

int main()
{
    foo f1(1, "abc",50);
    foo f2(100, "abcde",60);

    cout << resetiosflags(ios::adjustfield);
    cout << setiosflags(ios::right);
    cout << setw(6) << "i" << setw(15) << "s" << setw(15) << "j"<<endl;

    cout << setw(8) << f1.i << setw(15) 
        << resetiosflags(ios::adjustfield) << setiosflags(ios::left) << f1.s <<setw(5)
        << resetiosflags(ios::adjustfield) << setiosflags(ios::right) << setw(15) << f1.j << endl;

    cout << setw(8) << f2.i << setw(15) 
        << resetiosflags(ios::adjustfield) << setiosflags(ios::left) << f2.s <<setw(5
        << resetiosflags(ios::adjustfield) << setiosflags(ios::right) << setw(15) << f2.j << endl;

    /*i              s              j
          1abc                         50
        100abcde                       60*/

    return 0;
}

This is the output:

        i              s              j
          1abc                         50
        100abcde                       60

And this is what i need:

        i              s              j
          1             abc            50
        100             abcde          60

CodePudding user response:

Using left and right in the same line isn't a problem. It looks like the issue you have is not allowing for space after the first value. It looks like the setw(5) may have been for that, but since there's nothing printed after it there's no effect. I used 7 to match the 15 total used for the string width.

Maybe something like this would work? Probably best to extract the magic numbers into constants so you can adjust all of them easily in one place. You could also wrap this in an operator<< to contain all the formatting code in one place.

The first line headings offset one to the left looks weird to me, but it matches your example and is easy to adjust if necessary.

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

class foo
{
public:
    int i;
    std::string s;
    int j;
    foo(int i1, std::string s1, int j1) : i(i1), s(s1), j(j1)
    {};
};

int main()
{
    foo f1(1, "abc", 50);
    foo f2(100, "abcde", 60);

    cout 
        << right << setw(7) << "i" << setw(7) << ' '
        << left << setw(15) << "s"
        << right << "j"
        << endl;

    cout 
        << right << setw(8) <<  f1.i << setw(7) << ' '
        << left << setw(15) << f1.s
        << right << f1.j
        << endl;

    cout 
        << right << setw(8) << f2.i << setw(7) << ' '
        << left << setw(15) << f2.s
        << right << f2.j
        << endl;

    return 0;
}

Output:

      i       s              j
       1       abc            50
     100       abcde          60
  • Related