53176

How to use carriage return with multiple line?

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?

Answer1:

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.

Answer2:

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.

Recommend

  • Elements in random order and maxOccurs > 1
  • Sending mail with ASP.NET
  • Capture Console output of Spark Action Node in Oozie as variable across the Oozie Workflow
  • Add Special Case to Power BI Query (M)
  • C# “cannot assign field because it is a foreach iteration variable”
  • Problem in concatenation of objects in javascript
  • Creating PDF from TIFF image using iText
  • vectorized indexing/slicing in numpy/scipy?
  • cordova is not defined - cordova.js has already been loaded :: Ionic
  • jQuery: add elements until a particular height is reached
  • Combining two different ActiveRecord collections into one
  • as3-flash: any way to access all the instances placed in different frames from document class?
  • Silverlight DependencyProperty.SetCurrentValue Equivalent
  • What's the purpose of QString?
  • ViewController With Transparent Background Entering Current ViewController With Push Transition
  • What does 'Language neutral' mean with regard to MAKELANGID?
  • Excel's Macro-Recorder usage
  • Assign variable to the value in HTML
  • Cancel a live stream “fast motion” catch-up in Flash
  • Control modification in presentation layer
  • Linq Objects Group By & Sum
  • QLineEdit password safety
  • Optimizing database types to compact database (SQLite)
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Perl system calls when running as another user using sudo
  • Weird JavaScript statement, what does it mean?
  • Adding custom controls to a full screen movie
  • Apache 2.4 - remove | delete | uninstall
  • Confusion with PayPal's monthly billing cycle
  • How do I rollback to a specific git commit
  • Cannot Parse HTML Data Using Android / JSOUP
  • Matrix multiplication with MKL
  • Buffer size for converting unsigned long to string
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • How do you join a server to an Active Directory (domain)?
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • Understanding cpu registers
  • need help with bizarre java.net.HttpURLConnection behavior
  • reshape alternating columns in less time and using less memory
  • Converting MP3 duration time