A
A
avion2020-05-13 23:54:52
C++ / C#
avion, 2020-05-13 23:54:52

Output to C++ terminal?

Hello, why doesn't '\b' work for '\n'?

#include <iostream>

int main() {
      std::cout << "word\n" << "\b\b\b\b\b";
      return 0;
}

The result will be: word
Why does '\b' work for regular characters but not for '\n'?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Linuxoid, 2020-05-21
@orahorn

Because \n translates to a new line, and \b can no longer return to it and overwrite something - it is limited to its own line.
You without \n text word just rewrites the command line prompt.
It looks like this code does what you need:

#include <iostream>
  
int main() {
      std::cout << "word" << "\b\b\b\b    \n";
      return 0;
}

There are 4 spaces after the final \b (they will overwrite the text), and then \n will move the cursor to a new line.
And on this new line, the interpreter will issue an invitation without rewriting anything.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question