Trying to understand the \b escape code

So I used the below code to get an output of "Tes":

1
2
3
4
5
6
7
8
9
10
11
12
// just playing with escape codes

#include <iostream>
using namespace std;

int main ()
{
	cout << "Test";
	cout << '\b';
		
	return 0;
}


But when I use the below code, I get "Test" as an output:

1
2
3
4
5
6
7
8
9
10
11
12
13
// just playing with escape codes

#include <iostream>
using namespace std;

int main ()
{
	cout << "Test";
	cout << '\b';
	cout << '\n';	
	
	return 0;
}


If I had to guess, it has something to do with the newline code. What am I missing here? :)
closed account (o3hC5Di1)
Hi there,

Can you try the following please:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main ()
{
	cout << "Test\b";
	cout << '\n';	
	
	return 0;
}


All the best,
NwN
What I'm guessing is, your output on the first program, looks like this.. TesPress any key to continue

All the escape code '\b' does, is moves the cursor back one space, it doesn't remove text from the screen. So all that is happening is the cursor was moved back one space before the text of "Press any key.. " was displayed, wherein the second program just has the cursor moving back one, then a newline. To actually remove the text, you need a space after the '\b', as cout << "\b ";
Aaahhh. I see. I thought it actually removed text. That makes much more sense now.

Thank you for your time!
Topic archived. No new replies allowed.