std::cout to print character N times

前端 未结 3 1295
星月不相逢
星月不相逢 2020-12-29 03:32

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

3条回答
  •  灰色年华
    2020-12-29 03:49

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

提交回复
热议问题