My question is, how do I get the numbers 10 – 0 to print out on the same line, overwriting each other using either a WIN32 or GNUC compiler in a simple manner like my code below:
This is what I have so far:
#include <iomanip>
#include <iostream>
using namespace std;
#ifdef __GNUC__
#include <unistd.h>
#elif defined _WIN32
#include <cstdlib>
#endif
int main()
{
cout << "CTRL=C to exit...\n";
for (int units = 10; units > 0; units--)
{
cout << units << '\r';
cout.flush();
#ifdef __GNUC__
sleep(1); //one second
#elif defined _WIN32
_sleep(1000); //one thousand milliseconds
#endif
//cout << "\r";// CR
}
return 0;
} //main
But this only prints:
10
9
8
7
6
5
4
3
2
1
I did some really trivial modification (mostly just to clean it up and make it more readable):
This worked fine with both VC++ and Cygwin. If it’s not working under mingw, it sounds to me like an implementation problem.