Need help w/ a Qbasic LOCATE like command for C++
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!
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.
Good point, but shouldn't something like SetConsoleCursorPosition be pretty standard? Speaking of. I still can't get it to work.
there's more to the program, but that's just the area currently in question. Any info on what I'm doing wrong?
#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?
Well you need to actually assign hStdout to a handle to the console. The following compiled and ran as expected for me:
No. It is only availible on Windows. You might try a console library like curses, which, I believe, has ports to various platforms.
- Pete
#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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement