我只是想写一个程序,在控制台屏幕的同一行输出一系列相互覆盖的数字。像 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
是错误的。
cout << '/r'; // CR
是 2 个字符(即屏幕上打印的 12146
)。正确的值必须是
cout << '\r'; // CR
关于c++ - 带暂停的简单 C++ 循环,但输出非常奇怪!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5451826/