C++ Console Applications: setup and compilation
What is a Console Application?
While originally C++ Builder was developed for writing Windows applications
with a "Graphical User Interface" (GUI), you can also use it for Console
Applications. Worldwide, teachers use these pure text programs for teaching
C++. Back to the old pre-Windows days!
When you start a console application, Windows creates a text-only mode console window through
which the user can interact with the application.
Console applications do not use the visual controls of the Visual Controls Library (VCL).
So, since there are no "forms", there will be no .dfm files.
A console program is not a DOS program, because it also can call Windows functions.
A simple example
Proceed as follows:
- Click menu option File / New / Other....
- Select Console Application
In the dialog that appears, change nothing, simply click OK.
- In the Project Manager panel (top-right part of the IDE window), right click Project1.exe,
next select Rename and rename it to ConsoleTest.
- Save your files in an empty folder of
your choice: menu File / Save All.
- In the Project Manager, double click on File1.cpp. In the Code Editor, note the almost empty unit that C++Builder has prepared.
- Add the lines that are marked in red:
typedef char _TCHAR;
#define _tmain main
int _tmain(int argc, _TCHAR* argv)
cout << "This is a console application.\n\n";
cout << "Enter your name and press ENTER: ";
cin >> YourName;
cout << "Hello, " << YourName << "!\n\n";
cout << "Press ENTER to continue...\n";
- Compile and run your program: press function key F9 (or select menu
Run / Run).
C++ Builder compiles the program and afterwards runs it in a console window.
- Close the console window.
- Stop C++ Builder.
- Check how your program runs as a stand-alone application:
- in your favorite file manager (for example Windows' Explorer), navigate to the folder where you saved your project, next navigate to the folder Win32/Debug, and double click the
- or open a DOS-window ("go to the command-prompt"), navigate to the correct directory, type ConsoleTest and press ENTER.
Part 2: Structure of the program; Input and Output