c++ – I don't understand the use of std :: setw

Question:

I'm doing an exercise in a C ++ book that I use to learn the language, and I can't quite understand the use of std :: setw and its use in conjunction with std :: left (the default) and std :: right . In fact, I don't know how, I have found the key to do what I wanted to do, but I don't understand why it works.

Tell me if I'm wrong, please, but according to what I think:

  • The for j loop causes the first triangle of * to unfold.
  • The line cout << right << setw (16 – i); it causes the b's of the * to be separated in each line to the right of each *, in 16 -i spaces, starting from the immediate character of each *.
  • The for z loop adds the triangle made up of letters b.
  • The instruction cout << right << setw (4 + i * 2); It is where I already get lost, because I could not get that result to come out, which is the one I want, and do not tell me why, if it was intuition or what, but I decided to put the "* 2" and so if it came out xD

Please, I know it's a lot to ask, but if someone could explain to me why it works and why I mess up so much with that instruction, I would greatly appreciate it.

    #include <iostream>

    using std::cout;
    using std::endl;

    #include <iomanip>

    using std::setw;
    using std::right;
    using std::left;

    int main() {
        for(int i = 1; i <= 10; i++) {
            for(int j = i; j >=1; j--)
                cout << '*';

            cout << right << setw(16 - i);

            for(int z = 11 - i; z >= 1; z--)
                cout  << 'b';

            cout << right << setw(4 + i * 2);

            for(int y = 11 - i; y >= 1; y--)
                cout << 'a';

            cout << endl;
        }

        return 0;
    }

I also put the output that it gives me, in case it was not portable.

*              bbbbbbbbbb     aaaaaaaaaa
**             bbbbbbbbb       aaaaaaaaa
***            bbbbbbbb         aaaaaaaa
****           bbbbbbb           aaaaaaa
*****          bbbbbb             aaaaaa
******         bbbbb               aaaaa
*******        bbbb                 aaaa
********       bbb                   aaa
*********      bb                     aa
**********     b                       a

Answer:

If I understood your question cout << right << setw(4 + i * 2); form the pyramid of spaces in the middle because the space that you increase to 4 by the sum of i is doubled, think that if you had this instruction cout << right << setw(4 + i); your result would be something like:

*              bbbbbbbbbb    aaaaaaaaaa
**             bbbbbbbbb     aaaaaaaaa
***            bbbbbbbb      aaaaaaaa
****           bbbbbbb       aaaaaaa
*****          bbbbbb        aaaaaa
******         bbbbb         aaaaa
*******        bbbb          aaaa
********       bbb           aaa
*********      bb            aa
**********     b             a

By multiplying i by 2 the space you leave for each line between b and a is doubled, when i = 1 will leave 6 spaces, on the next line it will leave 8 and so on progressively.

Scroll to Top