C++ Builder Tutorials

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:

  1. Click menu option File / New / Other....
  2. Select Console Application
    In the dialog that appears, change nothing, simply click OK.
  3. In the Project Manager panel (top-right part of the IDE window), right click Project1.exe, next select Rename and rename it to ConsoleTest.
  4. Save your files in an empty folder of your choice: menu File / Save All.
  5. In the Project Manager, double click on File1.cpp. In the Code Editor, note the almost empty unit that C++Builder has prepared.
  6. Add the lines that are marked in red:
    #pragma hdrstop
    #pragma argsused
    #include <iostream.h>
    #include <conio.h>
    #ifdef _WIN32
    #include <tchar.h>
    #else
      typedef char _TCHAR;
      #define _tmain main
    #endif
    
    #include <stdio.h>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      char YourName[20];
      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";
      getch;
      return 0;
    }
  7. 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.
  8. Close the console window.
  9. Stop C++ Builder.
  10. 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 file ConsoleTest.exe
    - 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

See also:

  Part 2: Input/Output