Lab 04: Input, output, arithmetic, and debugging with GDB

Jan 16, 2024  β”‚  m. Jan 14, 2024 by Charlotte Curtis

Objectives

Once more, this lab does not have an assessment component or starter code, but you are encouraged to add a new subdirectory to your lab repo and use it to store your work.

Using GDB

Debugging with GDB isn’t quite as easy as using a GUI debugger, but it works in a similar manner. To debug with gdb, follow these steps:

  1. Compile your program with the -g flag. This will include debugging information in the executable.
  2. Run gdb on your executable: gdb ./a.out (or whatever your executable is called).
  3. Set a breakpoint at the line you want to start debugging, e.g. break 10 (sets a breakpoint at line 10). You can also use b as a shorthand for break.
  4. Run your program with run.
  5. When your program hits the breakpoint, the execution pauses and you can examine the state of your program using:
    • print (or p) to print the value of a variable or expression
    • list (or l) to list the source code around the current line
    • next (or n) to execute the next line of code
    • display (no shorthand) to print the value of a variable or expression every time the program stops

You can add as many breakpoints as you want, and you can remove them with delete (or d) followed by the breakpoint number. You can also remove all breakpoints with delete breakpoints.

You can also run gdb from within emacs by typing M-x gdb (i.e. Alt-x followed by gdb). This will open a new window with a gdb prompt. You can then run gdb commands as normal. It’s pretty handy for viewing your source code while debugging, but does require a bit more emacs skill (such as using C-x o to switch between windows).

Exercises

For each of the following exercises, create a new source file with the following boilerplate:

#include <iostream>
using namespace std;

int main(){
    // your code here
    return 0;
}

Since there’s a fair bit of repetition, I recommend creating a template file and copying it for each exercise. Alternatively, you can do everything in a single file and comment out the previous exercises as you go.

Try practicing using GDB if you run into unexpected behaviour. Even if you don’t, it’s a good idea to get comfortable with it.

1. GDB test drive

Compile your pizza cost sharing program from the last lab with the -g flag and put a breakpoint at the start of the main function. Run the program with gdb and step through line by line. When you get to a variable that you’ve defined, try printing its value with p or display.

you can also use info locals to print all local variables.

2. Input, processing, and output

Create a program that computes distance traveled given a speed and a travel time. Choose a meaningful name for your new program.

Your program should behave as follows, where bold text indicates sample user input (i.e. it is not part of the program):

$ Enter the speed (km/h): 4
$ Enter the travel time (hours): 10
$ Your travel distance is 40 km.

Think about what data types are appropriate for the variables you need!

3. More fun with math

Create a program to convert a user-entered temperature in Fahrenheit to Celsius.

The formula to convert between Fahrenheit (F) and Celsius (C) is:

$$C = \frac{5}{9}(F - 32)$$

Again, pay attention to data types!

Test your program by running the program for some reasonable values from this table:

http://www.texloc.com/closet/cl_cel_fah_chart.html

Does your program generate the expected results?

4. Copy and modify

Revise the program from the previous exercise to use integers instead of doubles.

Once your program compiles, re-run the program using the same values used in exercise 8. How do these results correspond to your expected results? If they don’t correspond, why does this happen?

5. Remember the modulo operator?

Develop an algorithm that reads from the user a number of seconds and converts and outputs the corresponding number of hours, minutes and seconds. It is highly recommended to develop your algorithm on paper in pseudo-code before translating it to C++. The solution does not need if statements or loops!

As you are designing the algorithm you should generate some data you can use to test your algorithm. This data should consist of a number of values that result in different results.

Your program should behave as follows:

$ Enter the number of seconds: 75
0h:1m:15s
$


Previous: Lab 03: C++ Basics
Next: Lab 05: Functions