SetConsoleCursorPosition

Cliff_Heath
Cliff_Heath used Ask the Experts™
on
I'm trying to change the location of where my output is displayed.
Here's the code.

HANDLE StdOut = GetStdHandle(STD_OUTPUT_HANDLE);

COORD myCoord;

cout << "Hi";
myCoord.X =+ 20;
SetConsoleCursorPosition(StdOut, myCoord);
cout << "Bye";

The final display is HiBye. Why doesn't isn't the position changed?

Oh yeah I don't want to solve it with  cout << "                                   Bye";

Thank you.

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
The call to SetConsoleCursorPosition is supposed to set the cursor position to the location specified in the second parameter.  The code sets myCoord.X to 20 and doesn't do anything to myCoord.Y, so it contains garbage.  SetConsoleCursorPosition probably objected to the garbage.  Try initializing myCoord.Y to a reasonable value (such as zero for the top line).

Setting X and Y to specific values gives you absolute cursor positioning.  If you want to do relative cursor positioning, you first have to find out where the cursor is by calling GetConsoleScreenBufferInfo, then modify the values in the returned COORD structure with addition or subtraction to get a COORD structure to pass to SetConsoleCursorPosition.  If you were trying to add 20 to myCoord.X, you should have coded "+=" instead of "=+".

--efn

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: efn {http:#9245895}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial