std::cout to print character N times

对着背影说爱祢 提交于 2019-12-30 00:52:06

问题


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


回答1:


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




回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/7897163/stdcout-to-print-character-n-times

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!