shiraz
shiraz

Reputation: 1218

std::cout to print character N times

How can I print a character N number of times using std::cout without looping?

Is there a way to move the text cursor back to nullify the effect of std::cout << std::endl;? i.e. to move up a line (say we never printed anything after doing the std::cout << std::endl; operation).

Upvotes: 43

Views: 73803

Answers (3)

Benjamin Lindley
Benjamin Lindley

Reputation: 103733

std::cout << std::setfill(the_char) << std::setw(100) << "";

Upvotes: 29

Mawg
Mawg

Reputation: 40185

is there a way to back our way to nullify the effect of cout << endl; i.e. to move up a line(say we never printed anything after doing the cout << endl; operation) Thank you so much!

Use the ternary operator (or an if statement if you refer) ... something like ...

void PrintCharNtimes(char chatToPrint; int numTimes)
{
   std::cout << std::string(numTimes, chatToPrint) << (numTimes > 0) ? std::endl : ;
}

Upvotes: 0

sehe
sehe

Reputation: 393547

 std::cout << std::string(100, '*') << std::endl;

To move a line up, you have to resort to terminal escapes (assuming that isatty() indicates that you are running on one).

Upvotes: 82

Related Questions