如何更改输出的位置或光标在输出处,以便在"Code Blocks"中C++

How to change the location of output or the cursor at output, for C++ in "Code Blocks"

本文关键字:输出 Code Blocks C++ 何更改 位置 光标      更新时间:2023-10-16

嗨,编程爱好者,我在 windows17.12 上使用"代码块"ver10,现在想将输出结果带到第 10 行和第 10 列,所以我使用了上面的 IDE 给出错误的 gotoxy(( 函数。错误文本:"gotoxy"未在此范围内声明。 需要注意的是,在主函数之前,我把conio.h

然后我在谷歌上搜索语法gotoxy((来更改输出的位置或输出上的光标,但我没有得到结果。 感谢您在这方面指导我。

您可以创建自己的gotoxy()。看看这个程序结构:

#include <windows.h>
void gotoxy(int x, int y); // declaration
.
.
void gotoxy(int x, int y) {
static HANDLE h = NULL;
if (!h) h = GetStdHandle(STD_OUTPUT_HANDLE);
COORD c = {x, y};
SetConsoleCursorPosition(h, c);
}

或者,您可以输入:

#include <windows.h>
void gotoxy(int x, int y) { 
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

笔记:

  • 两个代码都必须#include <windows.h>必需的。
  • GCC 编译器不支持conio.h
  • 您无需删除任何头文件。