Create a sample program with Code::Blocks

Create a sample program with Code::Blocks

If you’re new to Code::Blocks IDE and you’ve already downloaded and installed the program, it’s a good time to create your first program. We’ll  go over the steps to create a specific starter program using Code::Blocks and programming in C++. This is a great way to get familiar with the layout and workings of the software.

Add files to your project

Follow these steps to add files:

  1. Go to the Project menu and select Add files. This will bring in the files associated with your program.
  2. Add any additional .cpp or header files to your project at this step.
  3. Name your files. Now you will see your main .cpp file (if programming in C++) as well as any other .cpp files that you have added to your project. Press Select All to have this file saved as both debug & release targets. Press OK when done.

Edit your files

  1. To begin to edit a file from your project, double-click the file name from sources, and it will appear in the window with line numbers.
  2. Write your code in the main.cpp file (or alternate .cpp file that you have chosen.)
  3. When testing your code, make sure that Debug is selected as the target to use. This is needed to have a debug version available once you compile your code.

Syntax

In C++ there is no special syntax for streaming data input or output. Instead, these are combined as a library of functions. Iostream provides basic input and output services for C++ programs; it must be included for your program to function properly. This is why we must include the #include< Iostream> line in our program.

Formatting the C++ code

We also must include the #include <cstdlib> to the program in C++ .
Be sure the statements you wish to print in the console are between “” quotation marks. For example:

cout << “Hello there!….or should I say NAMASTE “<<endl;

Be sure to use correct beginning and ending statements with each line of code. For basic C++ syntax please see this link.

Input the following code into your project (see the image above for comparison):

#include <iostream>
#include <cstdlib> //wxDev-C++ users may need this header file here to allow calls to system commands

using namespace std;

int main()
{
cout << “Hello there!….or should I say NAMASTE “<<endl;
cout << “Looks like it is going to be another warm summer day “<<endl;
cout << endl;
cout << “Time to get to the Maggini Hall labs where it is cool\n”
<< “and write some useful computer programs”<<endl<<endl;
cout << “C++ is a high-level programming language”;
cout << “…..LETS GIVE IT A GO!!!”<<endl;
cout << “your name here”<<endl;
// system (“PAUSE”); //if using MS Visual C++…not needed in Code::Blocks or XCode
return 0;

}

Then save your work.

Compiling

After you complete the writing and editing of your program, you must compile the file. To do this, first go to the Build menu and select Compile Current File (or Ctrl+Shift+F9).

Next, test your project. From the Build pull-down menu, click Build and Run. This step will build an executable file for you. A project build takes the compiled versions of your source files and combines them into one single program. Your program output will now be displayed. You will see all of the text you wrote in between the quotes displayed as program output.

If an error is displayed, check the syntax of your code and check for missing pieces, or unintentional errors.

Although this is a very simple program, it is the foundation for beginning to write more in-depth programs in Code::Blocks. As your programs increase in length and depth, the same basic steps apply. Happy programming!

Sources:
https://www.cprogramming.com/tutorial/lesson1.html

https://en.wikipedia.org/wiki/Code::Blocks

  • Link Copied!
Loading comments