I'm using c++ string with special characters for console output. Most of the results can be predicted, but one of them fell out of my expectation. I could not find answers anywhere.
Platform: Windows 7 Enterprise Version 6.1 (Build 7601: Service Pack 1) Compiler: g++ (GCC) 8.2.0, c++17
#include <iostream>
int main(){
using namespace std;
char numString[12] = "0123456789\n";
//This is group 1
numString[3] = '\t';
numString[4] = '\b';
cout << "Group 1 output:\n" << numString << endl;
//This is group 2
numString[3] = '\b';
numString[4] = '\t';
cout << "Group 2 output:\n" << numString << endl;
//This is group 3
numString[3] = '\n';
numString[4] = '\b';
cout << "Group 3 output:\n" << numString << endl;
//This is group 4
numString[3] = '\b';
numString[4] = '\n';
cout << "Group 4 output:\n" << numString << endl;
//This is group 5
numString[2] = '\b';
numString[3] = '\b';
numString[4] = '\n';
cout << "Group 5 output:\n" << numString << endl;
return 0;
}
The output in console:
Group 1 output: 01256789 Group 2 output: 01 56789 Group 3 output: 012 56789 Group 4 output: 012 56789 Group 5 output: 01 56789
The 4th group output is expected as,
Group 4 output: 01 56789
while the output actually is,
Group 4 output: 012 56789
What I can't understand is why the character '2' is still there.
Anyone could please help me understand the problem? Thank you.
After seeing the answers below, especially zar's, I believe I have understood the problem, and would like to summarize a bit here.
- Windows cmd console is in non-destructive mode when no physical keystrokes detected.
- Any new output starts overwriting the existing ones from the current cursor. Sounds redundant but necessary. If there is any new character output, it will overwrite the existing one until new characters are used up. If there are still more existing characters left, they will continue existing over there and may look like "output" behind the new characters.
- '\b' only moves the cursor one character back. It does not delete anything.
- '\n' only moves the cursor to the next line. It does not move any character behind it to the next line.
- '\r' only moves the cursor to the beginning of the current line.
Please pay attention to move the cursor.
I'd like to paste all of the code here:
//strwithspecialchar.cpp -- Understand special characters in C++ string
#include <iostream>
int main(){
using namespace std;
char numString[12] = "0123456789\n";
//This is group 1
numString[3] = '\t';
numString[4] = '\b';
cout << "Group 1 output:\n" << numString << endl;
//This is group 2
numString[3] = '\b';
numString[4] = '\t';
cout << "Group 2 output:\n" << numString << endl;
//This is group 3
numString[3] = '\n';
numString[4] = '\b';
cout << "Group 3 output:\n" << numString << endl;
//This is group 4
numString[3] = '\b';
numString[4] = '\n';
cout << "Group 4 output:\n" << numString << endl;
//This is group 5
numString[2] = '\b';
numString[3] = '\b';
numString[4] = '\n';
cout << "Group 5 output:\n" << numString << endl;
//This is group 6
numString[2] = '\b';
numString[3] = '3';
numString[4] = '\n';
cout << "Group 6 output:\n" << numString << endl;
//This is group 7
numString[2] = '2';
numString[3] = '\b';
numString[4] = '\a';
cout << "Group 7 output:\n" << numString << endl;
//This is group 8
numString[3] = '\b';
numString[4] = '\r';
cout << "Group 8 output:\n" << numString << endl;
//This is group 9
numString[3] = '\b';
numString[4] = '\n';
numString[8] = '\r';
cout << "Group 9 output:\n" << numString << endl;
return 0;
}
And the output below for better understanding these special characters:
Group 1 output: 01256789 Group 2 output: 01 56789 Group 3 output: 012 56789 Group 4 output: 012 56789 Group 5 output: 01 56789 Group 6 output: 03 56789 Group 7 output: 0156789 Group 8 output: 56789 Group 9 output: 012 967