Reputation: 6188
I have written a program that consists of multiples and multiples of function each of which are printing several things on the console when control is passed to them. Now I am trying to print everything in the center of the screen rather than on the upper left corner of the screen. For that purpose, the only thing I know is the gotoxy function of Windows.h. Now this would be an extremely hectic job because I would have to place gotoxy above each "cout". Is there a way that I set the cursor to a particular position on the screen and every time anything gets printed, the printing commences from that particular position.
Upvotes: 1
Views: 1576
Reputation: 131799
The following allows easy find-and-replace and takes care of setting the cursor to the center:
#include <iostream>
std::ostream& PrintCentered(){
// comment in the following if you're experiencing
// weird output due to io-buffering like @Ben says in a comment
//std::cout.flush();
gotoxy(your_x, your_y);
return std::cout;
}
Now just find&replace your std::cout
calls with the above function where you want it to be centered. Usage after replace should look like this:
PrintCentered() << "your message";
Upvotes: 1
Reputation:
Assuming you are using cout to write to, this hack should do the job:
#define AT_CENTER( stuff ) goto( 100, 100 ); cout << stuff;
Where 100, 100 should be replaced with your specific values. Then in use:
AT_CENTER( "The meaning of life is " << x );
Upvotes: 0
Reputation: 11585
cout
or any other stream-based I/O for drawing all over the screen. It doesn't make sense to "position" a stream if it's being redirected to a different device.Upvotes: 2
Reputation: 32429
Write a small helper function (e.g. printCentered(std::string) ) that receives the string to be printed. This function moves the cursor to the center and then prints the parameter. Then replace your couts with a call to this function.
Upvotes: 3