简体   繁体   English

更新打印在控制台 C++ 上的值

[英]Update value printed on console c++

How i can do to update values printed in the console in C++我如何更新 C++ 控制台中打印的值

Ex:_前任:_

Value Printed: 10打印值:10

now as I can update the printed value?现在我可以更新打印值了吗?

I've done something like this:我做过这样的事情:

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

My question is if there are any less horrific form?我的问题是是否有更不那么可怕的形式? thx.谢谢。

I would probably define a field class that stored its own position and value.我可能会定义一个field类来存储它自己的位置和值。 When you update the value, it updates the display appropriately:当您更新值时,它会相应地更新显示:

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

Then we could use this something like this:然后我们可以像这样使用它:

field<int> x(7, 0);

std::cout << "Please enter a number: ";
std::cin >> x;

Use your gotoxy() function but put it in a loop to make it jump back and then update.使用您的gotoxy()函数,但将其放入循环中以使其跳回然后更新。

Like in the code below..就像下面的代码..

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM