How to remove last character put to std::cout?

Xirdus picture Xirdus · Sep 19, 2010 · Viewed 47.5k times · Source

Is it possible on Windows without using WinAPI?

Answer

bjskishore123 picture bjskishore123 · Sep 19, 2010

You may not remove last character.

But you can get the similar effect by overwriting the last character. For that, you need to move the console cursor backwards by outputting a '\b' (backspace) character like shown below.

#include<iostream>
using namespace std;
int main()
{
    cout<<"Hi";
    cout<<'\b';  //Cursor moves 1 position backwards
    cout<<" ";   //Overwrites letter 'i' with space
}

So the output would be

H