Deleting string that was cout (printed) in cmd

I am trying to delete string from cmd but not all that is printed(cout)just that string is that possible to do?


1
2
3
4
5
6
7
8
9
10
11
12
13
cout << "TTTTTTT" << endl; //for example

/*
Now i cin string (str) and when i cin it and click enter can somehow str be deleted from cmd
*/

string str;
cin >> str;

cout << str;

// Now here some command that will delete that str that was printed in my cmd.
// Not system("Clear"); because it would delete everything i already printed 
Last edited on
You should be able to print '\r' to move the cursor to the beginning of the line in a console, such that you can overwrite what was already printed on that line. A sequence of backspace characters '\b' can also be used to move the cursor back toward the beginning of a line, one character at a time.

Otherwise, if you're trying to do something more complex than that, that's beyond the scope of what C++'s standard I/O is supposed to accomplish. For that, you'll need to look into a proper library for manipulating the console, be it Windows's Console API, ncurses, or something else. You'd then use that instead of the stuff in <iostream>.

-Albatross
Thanks.
there are libraries to do console output in colors and to jump the location of the cursor and so on as well. anyone can use ncurses and windows has gotoxy(x,y) and some other handy ones.
There's also ANSI escape codes: https://en.wikipedia.org/wiki/ANSI_escape_code

Supposedly, Windows supports these now. Maybe someone can try this on Windows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main() {
    std::cout << "hello\n";

    char ch;
    std::cin >> ch;  // hit a single char and enter

    // Clear the screen
    std::cout << "\x1b[H\x1b[J" << std::flush;

    std::cout << "world\n";
    std::cin >> ch;
}

hello
x
world
x

Nope. Maybe it's possible but some system calls are needed beforehand to make it compatible?
Either way I'd just use something like ncurses instead of abusing stdout streams.
(I used MinGW btw)

Edit: LOL, if I run that in PowerShell it brings up the Mouse Properties menu. Haha what?
Last edited on
It works for me on Win10 Pro x64 and VS 2019.

someone can try this on Windows

Done and done.
Doesn't Windows have two command prompts terminal emulators as of June, one of which is the POS that Windows has always had, and the other is actually sane?

-Albatross
Last edited on
PowerShell IMO is the red-headed step child. The Command Prompt is the one with more power and sanity.
Wikipedia wrote:
In 2016, Microsoft released the Windows 10 Version 1511 update which unexpectedly implemented support for ANSI escape sequences[12]. The change was designed to complement the Windows Subsystem for Linux, adding to the Windows Console Host used by Command Prompt support for character escape codes used by terminal-based software for Unix-like systems. This is not the default behavior and must be enabled by enabled programmatically with the Win32 API via SetConsoleMode(handle, ENABLE_VIRTUAL_TERMINAL_PROCESSING).[13] This was enabled by CMD.EXE but not initially by PowerShell[14] however, Windows PowerShell 5.1 now enables this by default.

https://en.wikipedia.org/wiki/ANSI_escape_code#DOS_and_Windows
Albatross is referring to the new Windows Terminal (https://devblogs.microsoft.com/commandline/windows-terminal-microsoft-store-preview-release/), which is different than the Windows Command Prompt.

I have yet to try it, but it promises full Unicode support, among other things. It must be downloaded from the Windows Store (or compiled from github sources).


As for OP’s problem, he is making a design mistake. The correct answer is don’t do that. Figure out a better way of making your program run.

If you are interested in basic terminal functionality on Windows and Linux, take a look at the example here, which will get you started with basics: (Game of Live:Gosper Glider Gun|http://www.cplusplus.com/forum/lounge/75168/)

Hope this helps.
Tried the standard Windows PowerShell and it fubars same as happened to Ganado.

Apparently VS 2019 and 2017 run special debug console windows that allow for the escape codes to work.

"Regular" command prompts don't.

Outta here.
So called “ANSI escape codes” are a subset of VT-100 terminal control codes, which have never worked on DOS/Windows without a TSR/driver.

As it is, Windows 10 Windows Console supports them, but you must use SetConsoleMode() to enable them. See the documentation for more:
https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences

I am not sure when this change was actually implemented. As you know, Microsoft wants to pretend that there is only One Windows and their most recent documentation has been lax in supplying minimum system requirements for stuff.

But, you shouldn’t be using terminal escape sequences to control the Windows Console from your programs anyway. I suspect, though uninformed (because I don’t care), that the introduction of escape sequence handling in the Windows Console has to do with the ongoing *nix/POSIX integration.
Topic archived. No new replies allowed.