Iostream cout on loop same line
How i can invoke cout to print on the same place .....?
same as linux comman curl -O http ******
and display 20% on console on the same place not new line.... or append on line...
Code:
#include <iostream>
using namespace std;
int main ()
{
int n;
cout << "Enter the starting number > ";
cin >> n;
while (n>0) {
cout << "%" << n << endl;
--n;
}
cout << "FIRE!";
return 0;
}
Re: Iostream cout on loop same line
Use "\r" (carriage return) to return to the beginning of the same line (without the line feed). Remember you have to flush stdout to see anything if you don't use "\n".
Re: Iostream cout on loop same line
Quote:
Originally Posted by
wysota
Use "\r" (carriage return) to return to the beginning of the same line (without the line feed). Remember you have to flush stdout to see anything if you don't use "\n".
so is corect ?
if the nummer is moore as 1200 i can see .... otherwise go to fast .....
only color i can not print......
on php i print \033[01;31m to console to display red on c++ console is not ansi?
Code:
#include <iostream>
#include <stdio.h>
using namespace std;
int main ()
{
int n;
cout << "Enter the starting number > ";
cin >> n;
while (n>0) {
cout << "%" << n << "\r";
fflush ( stdin );
--n;
}
cout << "FIRE!";
return 0;
}
Re: Iostream cout on loop same line
You can use ANSI codes in c++, too:
Code:
#include <iostream>
using namespace std;
extern "C" {
#include <unistd.h>
}
int main() {
int n = 0;
cout << " %";
while (n < 10) {
// move cursor to first column, print number, move cursor to fifth column
cout << "\033[1G" << n*10 << "\033[5G" << flush;
sleep(1);
n++;
}
// print in red
cout << "\nTest color: \033[1;31mred\033[0m" << endl;
}