Sign in to follow this  

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

This topic is 4843 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

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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?

Share this post


Link to post
Share on other sites

This topic is 4843 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this