• 13
• 16
• 19
• 27
• 9

# Need help w/ a Qbasic LOCATE like command for C++

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

## Recommended Posts

I'm trying to make an ASCII text game in C++ but I can't find a command that's similar to the Qbasic Locate command. I used gotoxy(x,y) a while back, but now it doesn't seem to work.. (I'm using MS-Visual Studio.NET instead of Dev-C++) So If anyone knows of a somewhat simple command that would help me place ASCII character on the screen w/o having to redraw the whole thing every time that would be great. Thanks!

##### Share on other sites
Keep in mind that any solution to your problem will be platform-specific, practically by definition. You might not care about that now, but at some stage, you will. So, get into the habit of using only what you can guarantee will be available, and trimming frills otherwise.

##### Share on other sites
Good point, but shouldn't something like SetConsoleCursorPosition be pretty standard? Speaking of. I still can't get it to work.

#include <iostream>#include <stdio.h>#include <conio.h>#include <windows.h>using namespace std;HANDLE hStdout, hStdin; CONSOLE_SCREEN_BUFFER_INFO csbiInfo; void titleScreen(){	  csbiInfo.dwCursorPosition.X = 15;   csbiInfo.dwCursorPosition.Y = 10;  system("cls");  SetConsoleCursorPosition(hStdout, csbiInfo.dwCursorPosition);   printf("%c", 2);}

there's more to the program, but that's just the area currently in question. Any info on what I'm doing wrong?

##### Share on other sites
Well you need to actually assign hStdout to a handle to the console. The following compiled and ran as expected for me:

#include "stdafx.h"#include <cstdio>#include <cstdlib>#include "windows.h"void gotoxy(HANDLE con, SHORT x, SHORT y){	COORD coord; 	coord.X = x; 	coord.Y = y;	SetConsoleCursorPosition(con, coord); }int main(){		HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); 	HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 	if (hStdin == INVALID_HANDLE_VALUE || hStdout == INVALID_HANDLE_VALUE) 	{		MessageBox(NULL, "GetStdHandle", "Console Error", MB_OK);		return 1;	}	std::system("cls");	gotoxy(hStdout, 15, 10);	std::printf("%c", '2');	return 0;}

Quote:
 Good point, but shouldn't something like SetConsoleCursorPosition be pretty standard?

No. It is only availible on Windows. You might try a console library like curses, which, I believe, has ports to various platforms.

- Pete

##### Share on other sites
Thanks! It worked great. Yeah I didn't know how to assign hStdout to the console.

Thanks again for all your help.

...Just out of curiosity, what would be a multiplatform way of doing the same thing?