Projects in Code::Blocks

Last modified: Dec 26, 2016
Contents:

The activities listed here presume you have access to the GNU g++ compiler with the Code::Blocks IDE. You will find these installed in the CS Dept. PCs. You can also find instructions on installing these free programs on the CS 333 Library page.

1 The Virtual PC lab

I’m going to assume, for the moment that you are not in a CS Dept lab, but are working from home or sitting in one of the general University (OCCS) computing labs.

Open a browser and go to the CS Dept home page. Look for the link to the “Virtual Computing Lab”. Follow the instructions given there under “Connecting” to open a Remote Desktop Connection to a virtual lab machine.

Log in with your account info as per the earlier Account Setup lab.

A few general cautions about working with virtual PCs.

Now if you are on campus and are in one of the CS Dept labs, you can, if you wish, log out of the virtual PC and complete the remainder of the assignment working directly on that PC instead of via a virtual machine. But even then, keep in mind that the virtual PCs are available to you from anywhere with an Internet connection.

2 Trying Out the Compiler Suite

The “official” compiler for this course is the Free Software Foundation’s g++ compiler. Without question, this is the most widely ported C++ compiler, available for almost all computing platforms. However, the compiler itself is just that — a compiler. Using it effectively generally requires some surrounding software, called an Integrated Development Environment (IDE), to provide code editors, project management support, and debugging support.

Luckily, there are several good-quality, free IDEs as well. In this course we will use Code::Blocks. The course Library page includes instructions on getting this, together with the g++ compiler, if you want to install it on your own PC. However, you will find it already in place on the CS Dept virtual PCs.

Run Code::Blocks (you should find this in the Start button menu on the CS lab PCs).[^ If you have installed it yourself, then the first time you 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 new installation of g++.)]

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.

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.

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

#include <iostream> 
#include <string> 

using namespace std; 

int main() 
{ 
  string greeting = "Hello world!"; 
  cout << 
  return 0; 
}

Save this and click the Build button (the blue gear) to attempt compilation. You should see some compilation errors due to the incomplete statement.[^ 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. ]

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:

helloWithString.cpp
#include <iostream> 
#include <string> 

using namespace std; 

int main() 
{
  string greeting = "Hello world!"; 
  cout << greeting << endl;
  return 0; 
}

Save and again click the Build button. This time things should succeed.

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

When you are satisfied, close that project.

3 A More Elaborate Program

Create a new project (still a Console Application) named “tempConvert”. 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.

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;
}

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. First, let’s set a breakpoint. 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.

From the “Debug” menu, select “Start”. The program will start running, but will pause at the line where you set the breakpoint. 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.[^
Unfortunately, I find that the current version has trouble displaying strings. ] 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.

Try using the “Next” button, a step at a time. 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.

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