Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::cout to print character N times

Tags:

c++

cout

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).

like image 217
shiraz Avatar asked Oct 25 '11 23:10

shiraz


People also ask

How do you print n times in C++?

std::cout to print character N times.

How do I print the same thing multiple times in C++?

You have to write a loop or initialize a std::string with a/2 '*' characters to get that working. Your logic to count number of stars that should get printed is correct.

How do I print a character in cout?

Then, how to print character? We can use cast type here, by casting into char we are able to get result in character format. We can use cout<<char(65) or cout<<char(var), that will print 'A'. (65 is the ASCII value of 'A').


1 Answers

 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).

like image 59
sehe Avatar answered Oct 11 '22 00:10

sehe