簡體   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