5

When I want to print out another text in the same line, I can do this:

int i = 0; string text = "Paragraph "; while (i < 10) { if (clock() % CLOCKS_PER_SEC == 0) { cout << text << i + 1 << "\r"; cout.flush(); i++; } } 

But, how I can I do this with multiple line? I want to retain a paragraph as a whole in its initial position in terminal. If I change text with a string that contains paragraph with some newline characters, it prints another new block of paragraph below the last printed.

How can I retain it's position?

3
  • Print the static (non-changing) text before the loop? Commented Mar 30, 2015 at 10:58
  • If you want a new line, print a line feed. Commented Mar 30, 2015 at 11:01
  • 1
    To restore a cursor position on another line, you'll need to use some character escape sequence supported by your terminal. The most primitive would be a clear-screen code, then re-rendering the entire screen. Most terminals support a targeted row/column movement sequence. ncurses is about the best option for a reasonably portable solution, but you may have enough portability from the ANSI save-cursor-position sequence \033[s and restore-saved-position \033[u. Commented Mar 30, 2015 at 11:05

2 Answers 2

6

Your question isn't very clear, but I'm going to assume you want to know how to overwrite text in places other than the current line.

Standard C++ doesn't give you this capability. You will have to use OS-specific functionality to place the cursor at an arbitrary place of the console.

Under Unix-like systems you will generally use ANSI escape sequences

Under Windows you're best served by the console manipulation functions, in particular SetConsoleCursorPosition. Look here for more console functions.

Sign up to request clarification or add additional context in comments.

Comments

1

It is not possible in standard C++.

The technique depends on what the standard output device (i.e. std::cout) is - which is difficult, as that depends on the operating system and choices by the end user. For example, a lot of physical terminals (and terminal/console emulators) support escape sequences. Standard output can be redirected to various devices (including to a text file, which makes positioning the cursor a bit pointless).

In general terms, you will need to specify the output device (i.e. what your program can assume output is being written to), the host system, system settings, and a bunch of other things. And then use an API (or library) supported on the host system. Depending on your choices here, the techniques are highly variable.

Under unix, functions libraries like curses might be used. If you use curses, it will probably be necessary to use other curses functions to actually write your output (rather than cout).

Under windows, there is a set of console API functions (a subset of the win API), such as SetConsoleCursorPosition(). Again, it might be easier if you use other console functions, rather than cout.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.