Control codes for the Quincy console

The C++ language does not provide special facilities for controlling the appearance of characters in the console window, because different systems vary so much in capabilities and required control codes. However, system-specific C++ libraries can provide very flexible control.

Full control over the console is possible in Windows, but tricky - search the internet for clear screen/console/mingw if you must; but maybe you should be thinking of a real Graphical User Interface (GUI).

Control chars

The following basic control chars work with the MSDOS console that Quincy provides. They are a legacy from the days when output went to a primitive electric typewriter called a teletype:

'\t'    tab to the next 8 character block
'\n'    newline (similar to endl)
'\b'    backspace
'\r'    return to start of current line
'\a'    bell
'\\'    \

In a C++ string, \ must introduce one of the control characters above, so that:

string s = "abc\tdef";
cout << s;

yields abc     def .

To get a backslash into the string as itself, it must be doubled:

string s = "abc\\def"

results in abc\def

Clearing the console

There is no simple code to clear the console window, but system("cls"); does the job. The system() call requires #include <cstdlib> .

Getting a char from the keyboard without hitting enter

getch() waits for a key to be hit and then returns an int which is the ASCII code representation of the character. The integer can be assigned to a char. Some keys (arrow and function keys) behave as if two successive keys have been pressed.

getche() works in the same way as getch() except that the character concerned is also echoed to the screen.

getch() and getche() require #include<conio.h>