Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

stdout

cout

Is it possible on Windows without using WinAPI?

like image 966
Xirdus Avatar asked Sep 19 '10 13:09

Xirdus


People also ask

How do you delete the last character in cout?

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.

How can I remove last character from a string in C++?

Using erase() function 1. Get the iterator to the last character and call the erase() function. 2. Pass the last character's index to the erase() function, which erases the last character.

How can I remove last character from STD string?

Use pop_back() Function to Remove Last Character From the String in C++ The pop_back() is a built-in function in C++ STL that removes the last element from a string. It simply deletes the last element and adjusts the length of the string accordingly.

How do I delete something from cout?

To clear the screen in Visual C++, utilize the code: system("CLS"); The standard library header file <stdlib. h> is needed. Note: If you wish to clear the screen after a cout statement, you will need to "flush" the iostream.


1 Answers

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

like image 73
bjskishore123 Avatar answered Sep 19 '22 15:09

bjskishore123