I
I
Ilya2014-09-14 12:41:58
C++ / C#
Ilya, 2014-09-14 12:41:58

How to use the setw manipulator in c++?

I started learning c++ using R. Laforet's book.
The book gives an example of using this manipulator:

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    long pop1=8425785, pop2=47, pop3=9761;
    cout << setw(9) << "Город " << setw(12) << "Население " << endl
    << setw(9) << "Москва" << setw(12) << pop1 << endl
    << setw(9) << "Киров" << setw(12) << pop2 << endl
    << setw(9) << "Угрюмовка" << setw(12) << pop3 << endl;
    return 0;
}

And the result of his work:
Город   Население
Москва    8425785
Киров          47
Угрюмовка    9761

For me, the result is a little different, i.e. the right column is not right-aligned:
Город Население 
Москва     8425785
Киров          47
Угрюмовка        9761
Program ended with exit code: 0

For compilation I used xcode on mac os.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikitos_STR, 2015-11-14
@Nikitos_STR

Try:
cout << setw(9) << "City" << setw(12) << "Population" << endl
<< setw(9) << "Moscow" << setw(12) << right << pop1 << endl
<< setw(9) << "Kirov" << setw(12) << right << pop2 << endl
<< setw(9) << "Glum" << setw(12) << right << pop3 << endl;

S
SuperMikhail, 2019-01-20
@SuperMikhail

Good afternoon. I also started to learn c++ from R. Laforet's book, and this problem also appeared.
Everything turned out to be trite. It's just that Xcode and other compilers do not work correctly with Cyrillic, so such shifts are obtained. If everything is in Latin, then there will be right alignment.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question