How to run your first program in the compiler?


Saving and compiling the program:

                    There are many C++ compilers available in the market. Some of them are freeware (meaning that they are free to use) while others have to be paid for. Turbo C++ compiler might be the simplest to use (but it is not freeware). Simply choose "New File" and type out the program coding. Suppose you are using some other compiler, click on File and choose "New". Some compilers may have the option of creating a C++ source file while other compilers may require a new project to be created. Whatever the method you will ultimately come to the step of creating a C++ source file. After typing the code in the compiler, save the file by giving it some name. The "Save As" option will appear under the "File" menu. Give a name (for example: first). Now the file is saved as first.cpp. All C++ source files are saved in *.cpp format. Just like *.doc represents a Word document file, *.cpp denotes a C++ (C Plus Plus) source file.

In the compiler program there will be an option called ‘Compile’ in the menu bar. Select the compile option and the compiler will do its work. It will compile the program (or in other words, it will read whatever has been typed) and in case there are any errors, the compiler will point out the line where the error was detected. Check whether the program has been typed exactly as given earlier. Even if a semi-colon is missing, it will lead to errors. If the compiler says no errors (or if the message "compiled successfully" appears), then you can go to the next stage: building the *.exe file.

*.exe file extension stands for executable files. A *.cpp file cannot be run directly on the computer. This has to be converted into a *.exe file and to do so select the "Make or Build exe" option in the compiler. The file ‘first.exe’ will be created. Now the program can be executed from the DOS prompt by typing ‘first’. But instead of running the program every time from DOS, it will be convenient to run the program from the compiler itself and check the output. In the compiler there will be another option called "Run". Just click on this and the program will run from the compiler itself (you needn’t switch back and forth between DOS and the compiler screen).

The figure should give you a rough idea as to how the executable file is created from the C++ source code.

 

The source code is the program that you type. Source code is converted into object code by the compiler. The linker will link your object code with other object codes. This process of creating a C++ program is discussed in the last chapter.


Modification that might be needed in first.cpp (depending on your compiler):

A little problem might be encountered while running your program from the compiler (this problem will exist in Turbo C++ compiler). While running the program from the DOS prompt, this problem will not occur.

What’s the problem? When first.cpp is executed from the compiler the program will ask the user to enter a character. Once a character has been entered, the program will return to the compiler screen. You won't see your output! It might appear as if there is some problem with the program. What happens is that the program displays the character on the screen, immediately terminates the program and returns to the compiler screen. This happens so fast that you can’t see the output being displayed.

Modify your program as shown below (if you are using Turbo C++):

            // Your first program modified: first.cpp

      # include <iostream.h>
      # include <conio.h>
      int main( )
      {
      char letter;
      cout<< "Enter any letter" ;
      cin>>letter;
      cout<< "The letter you entered is : " <<letter;
      getch( );
      return 0;
      }

 

                    Another header file called conio.h has been added. The function getch( ) is used at the end of the program. getch ( ) is defined in the conio.h header file. getch( ) stands for ‘Get Character’. When getch ( ) is executed, the program will wait till the user types a character (any character). Only after a character is typed will the program move to the next instruction. In the above program, there is no statement other than return 0; after getch ( ). Hence program flow is as follows:

The program asks the user to enter a letter. The user enters a letter. Then the program displays the typed letter on the screen. The program will not quit at this point since there is a statement getch ( ). It will wait till the user types another character. When the user presses a character the program will quit.

Type the above program, save and run it from the compiler. Some compilers have named the function getch( ) as _getch( ). This function also requires the conio.h header.

Remember: conio.h is not available with all compilers and getch( ) function is not a standard C++ function. Some compilers provide it but most do not. In other compilers if a similar problem is encountered while displaying the result try using

getchar( );

instead of

getch( );

getchar( ) function doesn’t require the conio.h header file. If the compiler does not recognize getchar( ), then instead of adding any of the in-built functions, just add another line as follows:

cin>>letter;

at the end of the program just before return 0;

The program flow will be the same as described earlier.

                    The latest compilers, like VC++ (Microsoft Visual C++ compiler) do not have any of the above problems even if the program is run from the compiler. VC++ will always ask the user to press a character to terminate the program.

Another alternative is to use a function called ‘system’, which is defined, in the header file: stdlib.h.

can be used to pause the program (i.e. execution of the program will continue only when the user presses a key).

can be used to clear the display screen. To use these two functions you have to type #include <stdlib.h> header file in your source code. The system( ) function actually executes a DOS command. (Try giving the commands ‘cls’ and ‘pause’ in your DOS prompt).


Go back to the Contents Page


Copyright © 2004 Sethu Subramanian All rights reserved.