gpt4 book ai didi

C++在控制台中移动光标

转载 作者:行者123 更新时间:2023-11-30 02:57:39 26 4
gpt4 key购买 nike

我正在使用 Visual Studio 2010,我正在尝试在用户按下键盘上的右阵列键时移动光标:

#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;
}

它可以工作,但只有一次 - 第二次、第三次等按不工作。

最佳答案

您永远不会更改代码中的 poz_x。在你的 while 循环中,你总是移动到初始值 +1。像这样的代码应该是正确的:

while(true)
{
Keys = getch();
if (Keys == 77)
{
poz_x+=1;
gotoxy(poz_x,poz_y);
}
}

关于C++在控制台中移动光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14404819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com