PDA

View Full Version : Iostream cout on loop same line



patrik08
7th December 2006, 10:18
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...




#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;
}

wysota
7th December 2006, 10:49
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".

patrik08
7th December 2006, 11:13
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?





#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;
}

danadam
7th December 2006, 14:12
You can use ANSI codes (http://en.wikipedia.org/wiki/ANSI_escape_code) in c++, too:

#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;
}