Jump to content
  • Advertisement
Sign in to follow this  
remmargorprogrammer

C++ right-justify loop output

This topic is 4431 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Don't know why you want for-loop, but here is simple Windows solution:


void Dummy(std::string const & rDummy)
{
CONSOLE_SCREEN_BUFFER_INFO pDummy = {NULL};

GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), & pDummy);

std::cout << std::setw(pDummy.srWindow.Right + 1) << std::setfill('\x20') << rDummy << std::endl;
}



It will only look okay if rDummy is less or equal to console width of course.

Share this post


Link to post
Share on other sites
This should work on a win32 system:


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

#include <windows.h>

int main()
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);

cout << setw(csbi.dwSize.X) << setiosflags(ios::right) << "hello" << endl;

return 0;
}

Share this post


Link to post
Share on other sites
I seem to remember having to do this in my programming class when I learned how to use formatting functions...ahhhh memories. Anyway, assuming you're printing a string, count the number of characters in the string to be printed, subtract that number from the value passed to setw, then pass that number into fill, then print the string.... should work....

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!