Basic C++ Projects in Code::Blocks

Last modified: Aug 18, 2020
Contents:

To practice programming in C++ you are going to need a C++ compiler. But a compiler is just a program that translates source code into object code. Very few people actually just a compiler by itself. Instead, they use an Integrated Development Environment, or IDE for short, a package of software that includes

In CS250, you will be using the Code::Blocks IDE, so to prepare for that course you should become familiar with it.

1 Getting the Code::Blocks IDE

1.1 Already taking ODU CS classes?

If you are already an ODU student taking courses in the CS Dept, you can use the Code::Blocks environment on the Department’s network.

To do this, you will need to

  1. Apply for a CS Dept network account (if you don’t already have one) by going to http://www.cs.odu.edu/ and clicking on the “Account Creation” link.
  2. Once you have your account, connect to the ODU CS Virtual Lab. This connects you to a Microsoft Windows machine that will run Code::Blocks.

Even if you are able to the use Virtual Computing Lab, however, you may actually find it more convenient to install your own copy of Code::Blocks.

1.2 Installing on your own PC

Code::Blocks is free software and runs under Windows, MacOS, and Linux. You can download it here.

2 Trying It Out

  1. Run Code::Blocks.

    If this is the first time you have run it, it may try to locate compilers it can work with. If so, select “GNU GCC Compiler”. Click “Set as default”, then “OK”. It should find your compiler.

  2. Let’s start out with a very simple project. Select “File->{}New->{}Project… Select ”Console Application“ and click ”Go“. Move through the Wizard, choosing C++ as the language. Give the project the title ”FirstProject“, click on the ”…" button to choose a place in which to save your work.

    Accept the rest of the defaults until the wizard closes.

  3. On the left you can now see your project structure. If you open the Sources folder, you can see that it has already created a basic main.cpp file for you. Double-click this to open it in the editor.

  4. Let’s make some changes before compiling this. Change the function to look like this:

    #include <iostream> 
    #include <string> 
    
    using namespace std; 
    
    int main() 
    { 
      string greeting = "Hello world!"; 
      cout << 
      return 0; 
    }
    
  5. Save this and click the Build button (the blue gear) to attempt compilation. You should see some compilation errors due to the incomplete statement.1

  6. Now put the cursor at the end of the “cout” line and type “greet” (without the quotes). Pause a moment and notice that a box pops up suggesting a possible completion for this variable name. Hit Tab to accept this suggestion. Now finish the program like this:

    greeting.cpp
    #include <iostream> 
    #include <string> 
    
    using namespace std; 
    
    int main() 
    {
      string greeting = "Hello world!"; 
      cout << greeting << endl;
      return 0; 
    }
    
  7. Save and again click the Build button. This time things should succeed.

  8. Click the Run button (the blue triangle) to execute your program.

3 A More Elaborate Program

  1. Create a new project (still a Console Application) named “tempConvert”.

  2. Remove the automatically generated main.cpp file (right-click on it and select “Remove file…”) from the project and add a new file (File -> New… -> File…) named convert.cpp.

  3. Edit it to look like this:

    #include <string>
    #include <iostream>
    
    using namespace std;
    
    // Farenheit to Centigrade converter
    int main()
    {
      double f, c;
      cout << "Enter a temperature measured in degrees Farenheit: "
           << flush;
      cin >> f; // reads f from the keyboard
      c = (5.0 / 9.0) * (f - 32.0);
      cout << "In Centigrade, that would be " << c << " degrees."
           << endl;
      return 0;
    }
    
  4. Try compiling and running this. If you have made any typos, you may get error messages when you compile. That’s OK, just make the fixes, save, and re-compile. (In fact, if you didn’t get any error messages, why not make a few deliberate mistakes just to get familiar with how the IDE behaves?)

4 The Debugger

Now, let’s suppose that this behavior was not what we wanted, but we weren’t sure why. We might want to run the program in a debugger.

  1. First, let’s set a breakpoint. A breakpoint is a location in our code where we would like execution to pause whenever we are debugging.

    In your temperature conversion project, find the line that starts: cin >>… Click just to the right of the line number. The small red dot indicates that a breakpoint has been set at this line.

  2. From the “Debug” menu, select “Start”. The program will start running, but will pause at the line where you set the breakpoint.

  3. You can now look at the state of the running program in some detail. For example, in the “Debug” menu, select “Debugging Windows”, then “Watches”. The window that pops up allows you to look at the value of variable.

    Click on the plus sign to open up the Local variables list.2

    You can use the various debugger buttons (the ones with the dark red curved arrows) to step through the code, a line at a time.

  4. Try using the “Next” button, a step at a time.

  5. By the way, don’t be surprised if c and/or f appear to contain garbage at first. That’s typical for an uninitialized variables. In fact, had I not wanted to improve the odds of your seeing that, I would have written the code like this:

    temperature.cpp
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    // Farenheit to Centigrade converter
    int main()
    {
      cout << "Enter a temperature measured in degrees Farenheit: "
           << flush;
      double f;
      cin >> f; // reads f from the keyboard
      double c = (5.0 / 9.0) * (f - 32.0);
      cout << "In Centigrade, that would be " << c << " degrees."
           << endl;
      return 0;
    }
    

    to minimize the time during which either variable is left uninitialized.

  6. Try making those changes, run the debugger again, and single-step through. Do you see any difference in how the variables behave?


1: If you see an error message that says you are using “an invalid compiler”, then Code::Blocks did not actually locate the compiler properly. From the “Settings” menu, select “Compiler and Debugger”, On the tab “Toolchain excutables”, Use the “…” button for the “Compiler’s Installation Directory” to point it to the directory where you installed MinGW.

2: Unfortunately, I find that the current version has trouble displaying strings.