Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ total right alignment in output

Tags:

c++

shell

my problem is the following: I would like to align an output text to the total right of my output shell when executing. I can not use the standard std::setw() and std::right manipulator, because I wish the output is always at the right boundary, no matter how much large is the shell.

Is there a way to achieve this?

like image 641
Teocuz Avatar asked Dec 06 '25 10:12

Teocuz


1 Answers

As commented by @Angew, output is a stream, so it doesn't have a total right. To achieve the goal, you can simply get the width of the shell first, and then use setw() and right:

CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
int width = csbi.dwSize.X;

string str = "string you want to print";
cout << right << setw(width) << str << endl;
like image 59
herohuyongtao Avatar answered Dec 07 '25 23:12

herohuyongtao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!