Unable to move cursor to top

After reading Duoas' post about clearing the screen I'm unable to bring the cursor to the upper left-hand corner. I've included the windows.h in my header. I tried to make it easier by combining the Standard Way with Windows API.

I'm able to clear many lines at once but unable to move the cursor back to the top. My code snippet is this:

cout << string (30, '\n');
HANDLE hStdOut;
COORD homeCoords = {0, 0};
SetConsoleCursorPosition (hStdOut, homeCoords);
run ();

When the game runs again, it clears the 30 lines like it should, but the text starts from the bottom, so I know the cursor didn't go to {0, 0}. Does anybody know why?

I hope I don't have to do the full Windows API method as that way looks complex and intimidating, and I don't feel ready to tackle that yet, this is why I just took the portion that's supposed to move the cursor back to the top.

If I can't combine the methods I'll just continue using many '\n' if player chooses to play again.

Any help or advise would be appreciated.
hStdOut is uninitialised. Initialise it with GetStdHandle().
https://msdn.microsoft.com/en-us/library/windows/desktop/ms683231(v=vs.85).aspx
Okay, thanks. I'll research this.
I have this now:

cout << string (30, '\n');
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD homeCoords = {20, 20};
SetConsoleCursorPosition (hStdOut, homeCoords);
run ();

It's initialized, and it works...somewhat, but the 0, 0 coordinates doesn't bring the cursor to the upper-left corner; it looks like it's off by about 10 rows and columns. The coordinates 20, 20 is kind of close but when replaying it a 2nd time it looks like the previous text hasn't been cleared. Then if I maximize the console window and bring it back down to size the previous text is cleared, or vise-versa. This is interesting. I'll need to continue experimenting and testing this.
After testing this I do not think this will work. Using multiple endl or '\n' merely adds space but doesn't erase what's already on the console like what using the full Windows API method does.

So let's say once you make 35 new lines, and use the homeCoords at 0, 0 you are back at the top of the console and the previous game text is visible. If you change the coordinates to be below that text then by the 3rd game the previous game text is visible again. You will need to find a way to keep changing the coordinates once a player accepts a new game. I don't think this is practical.

Ideally, I would like to see results that system ("CLS") gives (that is, starting a new game at the top of the console window rather at the bottom, and all console text is erased) but without that "bad" programming practice of using system (). Using the full API looks challenging, so for the time being I'll keep using cout << string (30, '\n'); or something, and deal with 2nd, 3rd, etc... games starting at the bottom.

Thanks integralfx for your help.
Topic archived. No new replies allowed.