I'm just trying to write a program which outputs a series of numbers overwriting one another on the same line of the console screen. like 10 9 8 7 6 etc.
I'm using xcode and compiling in xcode. And this outputs "10 121469 121468", what am I doing wrong? Why doesn't it seem so obvious?
#include <iomanip>
#include <iostream>
using namespace std;
#ifdef __GNUC__
#include 开发者_C百科<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
I don't know if this answers your answer but I've seen that your CR
is wrong.
cout << '/r'; // CR
is 2 characters (which is your 12146
printed on the screen). The correct value must be
cout << '\r'; // CR
This line is wrong:
cout << '/r'; // CR
That's two characters, you want
cout << '\r'; // CR
On n*x I use the following ANSI escape code to delete the current line and move the cursor to the beginning.
\033[0F\033[2K
So you would use it in the following way:
cout << "\033[0F\033[2K" << units << endl;
On the following page you can peruse all the details:
http://en.wikipedia.org/wiki/ANSI_escape_sequences
There's also a link on that page for how to achieve similar effects for windows.
精彩评论