How to include two calls of >> in one setw?
Asked Answered
Z

4

5

Take this a minimal working example

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{

    cout    << setw(10) << "aaaaaaa"
            << setw(10) << "bbbb"
            << setw(10) << "ccc"
            << setw(10) << "ddd"
            << setw(10) << endl; 

    for(int i(0); i < 5; ++i){

        char ch = ' ';
        if ( i == 0 )
            ch = '%';
        cout << setw(10) << i
             << setw(10) << i << ch
             << setw(10) << i
             << setw(10) << i
             << setw(10) << endl;
    }
    return 0;
}

The output is

   aaaaaaa      bbbb       ccc       ddd
         0         0%         0         0
         1         1          1         1
         2         2          2         2
         3         3          3         3
         4         4          4         4

What I would like to do is to include << i << ch in one field of setw(10) so that columns are aligned properly.

Zymolysis answered 18/9, 2017 at 6:39 Comment(2)
Did you read documentation of setw ?Scapegrace
@BasileStarynkevitch, so the only solution in here is to concatenate into one string?Zymolysis
A
3

probably combine i and ch in one string, setw wouldn't accept this behavior natively

try this snippet

 cout << setw(10) << i
      << setw(10) << std::to_string(i) + ch;
Afroamerican answered 18/9, 2017 at 6:45 Comment(4)
I would like to avoid this approach if necessary.Zymolysis
@Zymolysis What was your motive for avoiding this behavior?Afroamerican
@Zymolysis So you want to avoid this approach, but accept it over others. Why?Greenhaw
@Greenhaw as I said if necessary. I've already solved the problem by performing concatenation. What I was looking for is a shortcut something like passing a flag but it seems the language doesn't support this functionality.Zymolysis
G
6

Since we are looking at either ' ' or '%' you can simply calculate statically.

cout << setw(10) << i
     << setw( 9) << i << ch
     << setw(10) << i
     << setw(10) << i
     << setw(10) << endl;
Greenhaw answered 18/9, 2017 at 6:47 Comment(1)
Very simple, very elegant.Convolvulus
A
3

probably combine i and ch in one string, setw wouldn't accept this behavior natively

try this snippet

 cout << setw(10) << i
      << setw(10) << std::to_string(i) + ch;
Afroamerican answered 18/9, 2017 at 6:45 Comment(4)
I would like to avoid this approach if necessary.Zymolysis
@Zymolysis What was your motive for avoiding this behavior?Afroamerican
@Zymolysis So you want to avoid this approach, but accept it over others. Why?Greenhaw
@Greenhaw as I said if necessary. I've already solved the problem by performing concatenation. What I was looking for is a shortcut something like passing a flag but it seems the language doesn't support this functionality.Zymolysis
Z
3

You need to concatenate them into one string, like this:

#include <string>
cout << setw(10) << std::to_string(i) + ch;

in general.

But if you know that i is one character you could use:

cout << setw(9) << i << ch;

which might the case for you, since i seems to be ' ' or '%'.

Ziwot answered 18/9, 2017 at 6:46 Comment(1)
Is this the only solution for this problem?Zymolysis
S
3

I'm not sure to understand your need.

You could use some std::ostringstream like e.g.

 std::ostringstream os;
 os << i << ch << std::flush;
 std::cout << setw(10) << os.str();

You could build a string, like James Maa answered

Scapegrace answered 18/9, 2017 at 6:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.