Sei sulla pagina 1di 9

C++ Programming SOE2158

Getting started with Dev-C++

1. AIMS ............................................................................................................................................................. 1

2. STARTING DEV-C++ ................................................................................................................................. 2

3. CREATING A SOURCE FILE................................................................................................................... 2

4. DEBUGGING YOUR PROGRAM ............................................................................................................ 4

Adding break points........................................................................................................................................ 6


Displaying variable values ............................................................................................................................. 7
Stepping in your code ..................................................................................................................................... 8

5. EXERCISES ................................................................................................................................................. 9

Exercise 1 - more 'hello' ................................................................................................................................. 9


exercise 2 - Ohm's law.................................................................................................................................... 9
exercise 3 - finding average ........................................................................................................................... 9

1. Aims

The aims of this workshop include:


S Get you started with Dev-C++.
S Learn how to edit and run simple C++ source code.
S Learn how to create and manage a simple console project with multiple source files.
S Learn how to debug your programs using the integrated debugger.

1
C++ Programming SOE2158

2. Starting Dev-C++

From the Start Programs Programming, Maths & Statistics Programs Dev-C++
menu, select Dev-C++ to start the development environment as shown in Fig. 1.

Fig. 1. Starting Dev-C++.

3. Creating a source file

Dev-C++ allows you to create simple source files that can be compiled and linked quickly.

1. From the File menu select New Source File or click on the New icon as shown in Fig.
2. This will open the text editor where you can enter your source code.

New source file

Fig. 2. Creating a new source file.


2
C++ Programming SOE2158

2. In the text editor type in the following code:


// This is my first C++ program

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
cout << "Hello World!" << endl;
getch(); // waits for the user to press a key

return 0;
}

Text editor

Fig. 3. Entering code in the text editor.

3. Save your file using File Save from the main menu. You can either save your source
file on the:
- hard disk (e.g. C:\temp), or
- on your personal U drive in an appropriate directory (e.g. U:\mycpp).

Note that the file name that you choose (e.g. myprog.cpp) will also be the name of the final
binary or executable file. Remember that all files stored on the root directory C:\ will be
deleted when you log out.

3
C++ Programming SOE2158

Note: all C++ should have an extension (.cpp); Dev-C++ provides this extension as the
default when you save your program files.

4. Compile and run your program by selecting Execute Compile & Run from the main
menu (shortcut: F9). A console (or DOS) window will open displaying the "Hello
World!" message as shown in Fig. 4. Press any key on your keyboard to close this
window and return to Dev-C++.

Fig. 4. Console window with program output.

4. Debugging your program


It is a known fact that sometimes developers make errors when making a program. Those
errors commonly called bugs can be very tricky to find and correct, thus the debugger has
been created to help developers investigate their program while it is running.

A debugger basically runs a program while keeping track of its functions, variables and
instructions. It is capable of stopping your program at a given moment, which is called
breakpointing. You can set breakpoints anywhere in your code: once your program reaches
that code during execution, the debugger will stop and let you examine the current data in
your program.

Dev-C++ includes the GNU GDB debugger in its development environment. To be able to
use this debugger, you first need to enable the linker to generate the necessary information
for debugging:
- Select Tools Compiler Options from the main menu.
- From the Compiler Options window click on Settings.
4
C++ Programming SOE2158

- Click on Linker and set Generate debugging information to Yes in the linker
options as shown in Fig. 5, and then click OK.

Fig. 5. Setting linker options to enable debugging.

Before we can use the debugger, make sure that all projects/files from the previous
examples are closed (File Close Project, File Close All). We are now ready to use
the debugger.

Create a new source file and type in and save the following C++ program:
cvalue.cpp
// Program that changes the values of two double variables

#include <iostream>

using namespace std;

int main()
{
double x, y;

// Initial values of x and y


x = 3.0;
y = 7.5;

cout << "Wait here ...";

// Changing variable values


x = 8.6;
y = 1.0;

5
C++ Programming SOE2158

return 0;
}

ADDING BREAK POINTS

You can use breakpoints to pause your program at a certain line of code. You can add a
break point either by:
- positioning the text cursor on the code line and selecting Debug Toggle Break
Point, or
- click the left mouse button on the gutter next to the required line of code. This will
highlight the required line with red colour as shown in Fig. 6.

1. Add a break point to the following code line: cout << "Wait here ...";

Break point

Fig. 6. Adding a break point in the source file.

2. Compile your program using Execute Compile.


3. Run the debugger by selecting Debug Debug menu (shortcut: F8).
4. Dev-C++ will warn you that your breakpoint was reached by changing the line colour to
blue.

6
C++ Programming SOE2158

DISPLAYING VARIABLE VALUES

After the break point is reached, you can display the values of your variables either by:
- clicking on the Add Watch button (shortcut: F4) in the Debug window displayed at
the bottom of Dev-C++, typing the name of your variable in the dialog and
pressing the OK button, or
- placing your text cursor on the required variable in the code in the text editor and
pressing F4, or
- pointing your mouse over a variable in your source code (if Watch variable
under mouse is enabled in Environment Options in the Tools menu) and it will
be added to the watch list, or
- right-clicking the mouse button in the Debug section of the Project Manager
window and selecting Add watch from the pop-up menu.

1. Add watches to the variables x and y in your program.


2. The watch list will be displayed by clicking on Debug in the Project Manager window as
shown in Fig. 7.

Watch list

Fig. 7. Adding and displaying variable contents in the watch list.

3. You can remove a variable from the watch list by left-clicking on it and then pressing the
Delete button on your keyboard, or by clicking the Remove watch button.

7
C++ Programming SOE2158

STEPPING IN YOUR CODE

Once a breakpoint has been reached, you can step into or progress your code in three
different ways:

- Next Step button (shortcut: F7): The debugger will step one instruction (line
of code) in your program

- Step Into button (shortcut: Shit+F7): The debugger will step one instruction.
If that instruction is a function call it will jump into it.

- Continue button (shortcut: Ctrl+F7): The debugger will continue the


execution of your program until another breakpoint is reached.

1. Click on the Next Step button and observe how the values of x and y change as the
debugger traces through the code as indicated in Fig. 8. The program then terminates
when the debugger reaches the end of main.

Variables changed

Fig. 8. Change of variable values in the watch list.

This exercise completes the introduction on using Dev-C++ to create simple console
applications.

8
C++ Programming SOE2158

5. Exercises

EXERCISE 1 - MORE 'HELLO'


Write a program that prints out a message "Hello, this is my first C++ program!". Then on a
new line, the program should print the message "What do you think?". You must include the
getch() function at the end of your program code.

EXERCISE 2 - OHM'S LAW


Write a program that prompts the user to enter the current (I) and resistance (R), and then
use these to calculate and display the voltage according to Ohm's law V = IR.

EXERCISE 3 - FINDING AVERAGE


Write a program that reads 3 real numbers from the user, calculates their average and then
displays the result on the screen.

Potrebbero piacerti anche