C++ 控制台c和x2B上打印的更新值+;

C++ 控制台c和x2B上打印的更新值+;,c++,windows,c++14,C++,Windows,C++14,如何用C更新控制台中打印的值++ 例:_ 印刷价值:10 现在我可以更新打印值了吗 我做过这样的事情: void CursorXY(int x, int y) { COORD coords = { x, y }; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coords); } int main() { int x = 0; cout << "Value: " <<

如何用C更新控制台中打印的值++

例:_

印刷价值:10

现在我可以更新打印值了吗

我做过这样的事情:

void CursorXY(int x, int y)
{
    COORD coords = { x, y };
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coords);
}


int main()
{
    int x = 0;
    cout << "Value: " << x << endl;
    cout << "Some any value!" << endl;
    gotoXY(7, 0);
    cin >> x;
    gotoXY(0, 2);

    GetMessage(NULL, NULL, 0, 0);
    return 0;
}
field<int> x(7, 0);

std::cout << "Please enter a number: ";
std::cin >> x;
void CursorXY(整数x,整数y)
{
COORD coords={x,y};
设置控制台或位置(GetStdHandle(标准输出句柄),坐标);
}
int main()
{
int x=0;

cout我可能会定义一个存储自己位置和值的
字段
类。当您更新值时,它会相应地更新显示:

template <class T>
class field { 
    int x;
    int y;
    int w;
    T value;
public:
    field(int x, int y, w = 0, T value = 0) : x(x), y(y), w(w), value(value) {
        redraw();
    }

    field &operator=(T const &new_val) { 
        value = new_val;
        redraw();
    }

    voi redraw() {
        gotoXY(x,y);
        std::cout << std::string(' ', w));
        gotoXY(x, y);
        std::cout << std::setw(w) << value;
    }

    std::istream &operator>>(std::istream &is, field &f) { 
        is >> f.value;
        redraw();
        return is;
    }
};
模板
类字段{
int x;
int-y;
int w;
T值;
公众:
字段(整数x,整数y,w=0,T值=0):x(x),y(y),w(w),值(值){
重画();
}
字段和运算符=(T常量和新值){
值=新值;
重画();
}
voi重画(){
gotoXY(x,y);
标准::cout>f.value;
重画();
回报是;
}
};
然后我们可以用这样的东西:

void CursorXY(int x, int y)
{
    COORD coords = { x, y };
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coords);
}


int main()
{
    int x = 0;
    cout << "Value: " << x << endl;
    cout << "Some any value!" << endl;
    gotoXY(7, 0);
    cin >> x;
    gotoXY(0, 2);

    GetMessage(NULL, NULL, 0, 0);
    return 0;
}
field<int> x(7, 0);

std::cout << "Please enter a number: ";
std::cin >> x;
字段x(7,0);
std::cout>x;
使用您的
gotoxy()
函数,但将其放入一个循环中,使其跳回 然后更新

就像下面的代码一样

#include <windows.h>
#include <iostream>
using namespace std;


void gotoxy(int x, int y);
void setcolor(WORD color);
void setForeGroundAndBackGroundColor(int ForeGroundColor,int BackGroundColor);
void clrscr();




int main(){
    int x = 0;

    while(true){
        gotoxy(7, 1);
        cout << "Value: " << x << "   "<< endl;
        cout << "Some any value!  " << endl;
        cin >> x; 
        gotoxy(1, 2);
    }

    return 0;
}

void setcolor(WORD color){
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
    return;
}



void setForeGroundAndBackGroundColor(int ForeGroundColor,int BackGroundColor){
   int color=16*BackGroundColor+ForeGroundColor;
   setcolor(color);
}




void gotoxy(int x, int y){
    COORD coord;
    coord.X = x; coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    return;
}




void clrscr(){
    COORD coordScreen = { 0, 0 };
    DWORD cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD dwConSize;
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    GetConsoleScreenBufferInfo(hConsole, &csbi);
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
    SetConsoleCursorPosition(hConsole, coordScreen);
    return;
}
#包括
#包括
使用名称空间std;
void gotoxy(int x,int y);
void setcolor(单词颜色);
void setforegroundbackgroundcolor(int-ForeGroundColor,int-BackGroundColor);
void clrsc();
int main(){
int x=0;
while(true){
哥特氧(7,1);

如果您不想使用特定于操作系统的函数,如
gotoXY(),则不能
,打印值后不加换行符,然后打印足够多的退格并覆盖它。@Barmar-这种方法在当时非常流行。啊,对于VT52/VT100时代……我认为它仍然用于不使用全屏模式的简单程序/脚本。使用ncurses库也是一种可能