【发布时间】:2011-03-27 19:37:37
【问题描述】:
我只是想编写一个程序,它输出一系列数字,在控制台屏幕的同一行上相互覆盖。比如 10 9 8 7 6 等。
我正在使用 xcode 并在 xcode 中进行编译。这输出“10 121469 121468”,我做错了什么?为什么看起来不那么明显?
#include <iomanip>
#include <iostream>
using namespace std;
#ifdef __GNUC__
#include <unistd.h>
#elif defined _WIN32
#include <cstdlib>
#endif
int main()
{
cout << "Description: This program will show you how much change" << endl;
cout << "you will need to complete a transaction using a already" << endl;
cout << "specified denomination" << endl << endl;
cout << "CTRL=C to exit...\n";
for (int units = 10; units > 0; units--)
{
cout << units << ' ';
cout.flush();
#ifdef __GNUC__
sleep(1); //one second
#elif defined _WIN32
_sleep(1000); //one thousand milliseconds
#endif
cout << '/r'; // CR
}
return 0;
} //main
【问题讨论】:
-
互相“覆盖”到底是什么意思?通常不可能在输出流上“备份”。一方面,CR 的转义序列是 \r,而不是 /r。
-
@debracey:实际上,在控制台上输出
\r转义序列(显然是 OP 打错了)返回光标回到行首。这就是 OP 如何“覆盖”控制台上的先前输出。