我創建了一個像倒計時一樣作業的程式,我遇到了一個錯誤:一切都列印得很好,直到秒數下降(counter.second)低于 10,然后它列印 90 而不是 9(或 09),80 而不是 8 等等在。
如果我洗掉“\ r”,則“剩余時間文本”將彼此列印“計數器”次數,沒有此錯誤,但這會填滿我的 cmd,這會更糟。
除此之外,一切都按預期作業。
有想法該怎么解決這個嗎?
#include <iostream>
#include <windows.h>
int main() {
std::pair<int, int> counter;
std::pair<int, int> aux;
std::cin >> counter.first;
std::cin >> counter.second;
while (1) {
aux.first = counter.first;
aux.second = counter.second;
while (aux.first >= 0 && aux.second >= 0)
{
std::cout << "\rTime remaining: " << aux.first << ":" << aux.second << std::flush;
Sleep(1000);
if (aux.second == 0) {
aux.first--;
aux.second = 60;
}
aux.second--;
}
PlaySound(TEXT("retro.wav"), NULL, SND_ASYNC);
}
return 0;
}
uj5u.com熱心網友回復:
好吧,看看:如果你寫
I like cats.
然后用
I like dogs.
一切安好。但是如果你用
I like.
然后是貓。保持未被覆寫。
I like.cats.
這就是發生的事情。你試著10
用9
. 0
遺骸被發現。例如,您可以在此之后使用空格來修復它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/470838.html