Using Escape Codes


Up until now, the only way you've seen to move down a line is by using endl, the mystical-move-down-a-line object. What if we told you there was another, perhaps even better, way to do it?

Well, there is, and it's the magic of escape codes. What's an escape code? Well, they are a bunch of codes you can put in the middle of a string that represent actions. They can produce a tab on the screen, move the cursor down a line, back the cursor up, even make the computer beep. They best part is, you put them right inside your strings, so it saves a little work.

Escape codes always begin with a "\" followed by a letter. The first one we'll try out is "\n", which moves the cursor down to the next line.

#include <iostream.h>

void main(void)
{
   cout << "Hello World!\n";
}

Notice how the "\n" takes the place of having to say "<< endl". Isn't that so much easier? Let's try using some more ones:

#include <iostream.h>

void main(void)
{
   cout << "Here is a Return (\n)   " << endl;
   cout << "Here is a Tab (\t)      " << endl;
   cout << "Here is a Backspace (\b)" << endl;
   cout << "Here is a Beep (\a)     " << endl;
   cout << "Here is a Slash (\\)    " << endl;
   cout << "Here is a Quote (\")    " << endl;
}

The above code should explain itself, but here is a list of escape codes you can use and what they mean:

\n            Newline
\t            Tab
\b            Backspace
\a            Beep
\\            Backslash
\?            Question Mark
\'            Single Quote
\"            Double Quote


Table of Contents

Structures | Pointing to Structures | Using Pointers to Structures | Using Escape Codes
Shortcuts | Exercises