In C++ I'm trying to go back up a line to add some characters. Here is my code so far:
cout << "\n\n\n\n\n\n\n\n\n\n\xc9\xbb\n\xc8\xbc"<<flush;
Sleep(50);
As you can see, I have 10 newline characters. In my animation, a new block will be falling from the top of the screen. But I don't know how to go back up those lines to add the characters I need. I tried \r, but that dosen't do anything and \b dosen't go up the previous line either. Also, what exactly does flush do? I've only been programming in C++ for about 2 days so I'm a newb =P.
Thanks so much!!!
Christian
Alt + ↑ (Up Arrow) – Moves a line up.
You can go to the beginning of the line by printing \r , but moving to the previous line requires platform dependent code. If don't want to use libraries like Curses, you can try ANSI escape codes. Depending on the terminal, cout << "\033[F" will move the cursor one line up.
The cout operator does not insert a line break at the end of the output. One way to print two lines is to use the endl manipulator, which will put in a line break. The new line character \n can be used as an alternative to endl. The backslash (\) is called an escape character and indicates a special character.
You can go back to your previous location with the Go > Back command or Ctrl+Alt+-.
If your console supports VT100 escape sequences (most do), then you can use ESC [ A
, like this:
cout << "\x1b[A";
to move the cursor up one line. Repeat as necessary.
In windows you can use this example
there you will create CreateConsoleScreenBuffer() and then are using SetConsoleCursorPosition(console_handle, dwPosition);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With