繁体   English   中英

如何在不破坏我的第一个程序的情况下制作第二个指令?

[英]How can I make a second cout without destroying my first in this program?

#include "stdafx.h"
#include "iostream"
#include "thread"
#include "conio.h"
#include "windows.h"
using namespace std;

void incrm();
void charget();

void main() {
    thread count(incrm);
    thread getcin(charget);

    count.join();
    getcin.join();

    cin.get();
}

void incrm() {
    int j = 0;    // used to increment and output
    while (true) {
        cout << "\r" << j;    // outputs 1,2,3,4... and so on
        j++;
        Sleep(150);
    }
    cout << endl;
}

void charget() {
    while (true) {
        int i = getch();    // gets value of char
        cout << "\r\nCHAR: " << i;  // and here is the problem...!
    }
}

因此,我希望该程序在第一行中输出一个数字,该数字在不停止的情况下递增,并且如果您敲击任何键,它应该在第二行中指出该键的值,因此我希望它输出这样的内容->

45
CHAR: 97

按下键后,递增数字应保持在第一行。 如果您按了几个键,第二个提示应该被覆盖,但是这似乎对我不起作用,如果我按了几个键,我的输出看起来像这样->

10
12AR: 97
20AR: 96

我的问题是我的第一个提示(递增的数字)会覆盖我的第二个提示(或者我不知道的第二个覆盖我的第二个提示),然后每一行的结果都如此! :(

我建议您使用称为gotoxy()的Windows函数。 您可以使用

SetConsoleCursorPosition();

Windows.h库中提供了此功能。 可以在这里阅读更多信息: 链接

暂无
暂无

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

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