I’m using Visual Studio 2010 and I’m trying move cursor when user press right-array key on keyboard:
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
void gotoxy(int x, int y)
{
static HANDLE h = NULL;
if(!h)
h = GetStdHandle(STD_OUTPUT_HANDLE);
COORD c = { x, y };
SetConsoleCursorPosition(h,c);
}
int main()
{
int Keys;
int poz_x = 1;
int poz_y = 1;
gotoxy(poz_x,poz_y);
while(true)
{
fflush(stdin);
Keys = getch();
if (Keys == 77)
gotoxy(poz_x+1,poz_y);
}
cin.get();
return 0;
}
It’s working but just once – second, third etc. pressed not working.
You never change
poz_x, so you always end up callingin the loop.