Saturday, January 14, 2012

Tutorial: How to Create My First Program in C++ using DevC++?

by eturo

To create a program it requires two special software i.e. editor and compiler. Editor is used to write source code and compiler to convert source code into the language that machines understands which is known as binary language.

An IDE or Integrated Development Environment - is one in which compiler and editor are combined into single package. After finishing with source code just click a button to create executable files. Dev-C++ is a development environment in which after writing the source code, we just have to press F9 and it complies and run both with just a single click.

I recommend you to download Dev-C++ because it is free and has all the features that you will need. Dev-C++ is a full-fledged public domain C++ environment. Dev-C++ supports the entire C++ language. It is an ANSI compliant. It has 32-bit compiler.

So feel free to download it from bloodshed.net. You will see various version of Dev-C++ there but download the latest one. (You can download the latest version here.)

Today, I will teach you how to create a basic or your first program in C++ using DevC++. 

Before anything else, do the following.... 

Step 1: Install first DevC++ into your computer. You can get it from the link posted above. You can also read the installation procedures in their official website. Here, I used the latest version (Dev-C++ 5 Beta 9.2 (4.9.9.2) of the software which was released in February, 21 2005 (matagal na actually). Please read the installation guide here.


Step 2: Now, after installing the compiler (Dev-C++), we are now ready to create our first ever (hehehe) program in C++. We will call or name it as "HelloWorld" in welcoming you to the world of C++ programming. Here is the sample output of the first program.

 

Now, to have a similar output just like above, below is the sample program source code with comments giving you some idea or information how the program works. You can copy it and paste it to your Dev-C++ compiler to compile and run.




The Program Source Code:

//include first all the header files
//it contains the libraries needed by the program
#include
#include

using namespace std;

//this will be the main program
int main()
{
    //functions "cout" use to display output to the screen
    cout<<"Hello World!"<    cout<<"\nThis is my first C++ program using Dev-C++...and I want to learn more!"<    cout<<"\nWelcome to my blogsite..."<   
    getch();
}



How to Compile and Run in Dev-C++?

To be able to see the result of the program, do the following steps:

Step 1: Open your Dev-C++ IDE compiler by clicking the shortcut in the desktop of click Start button, All Programs, select Bloodshed Dev-C++, then finally click Dev-C++ (see the icon image below) to launch the program compiler.




Now, if the compiler launched, this will the environment (see the image below).



If you are familiar in Java programming, you are definitely using IDEs just like Dev-C++  such NetBeans and Eclipse.


Step 2: Next, you are now ready to create a new file by clicking File >> New >> Source File or press CTRL + N for short.



Then the a white space will appear at the left part which will serve as the code are where you enter your source codes of your program.



The encircled number 1 as shown above indicate the line number of the codes. As you enter new line of codes, then it will automatically numbered.


Step 3: Here, you can now enter the text or by simply copying (CTRL + C) the sample program source codes given above...





and paste (CTRL + V)  it to the code area. (see the image below)



Step 4: We are now ready to compile our program, whether it contains errors or bugs or none. You should compile first your program before running it. Always do this... So to compile, go to the Execute menu then select Compile or press CTRL + F9. Then a message will appear, displaying of errors and warning during compilation of the program. (see the sample image below)



Here, it displays that there are no errors or warnings in compiling the source file. But, if some errors found in the program during compilation then error messages will be displayed below or bottom of the compiler as shown below:




Using the Dev-C++ IDE compiler, one of its nice feature is that it shows the line which contain the errors or bug from the code. So, this will be an advantage of the programmer to fix or debug the error codes.


Step 5: And finally, if you programs successfully compiled without errors, then it's time to see the result or output of the program. However, there are some circumstances that even the program successfully compiled, you cannot still see the output if there are some unexpected errors during running the program, we call these as run-time errors. So, you have to debug those parts of your program that trigger run-time errors.

To run your program, just go to Execute menu then click RUN or press CTRL + F10 from the keyboard. Then, the result will be displayed in console output terminal just like the image below.



Tip: To Compile and Run your program simultaneously, you can press F9 function key.



There you have it guys. It's easy to create, compile, run and even debug C++ programs using the Dev-C++ IDE compiler. You can also run programs to some other IDE compilers for C++ programs such as the Microsoft Visual C++ 6.0 and Codeblocks.


 

Try your first C++ program now! Happy coding....

Like Us on Facebook

Related Posts Plugin for WordPress, Blogger...