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).
std::cout to print character N times.
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.
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').
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With