C/C++ Programming 1 / C/C++ Programming 2
Ray Mitchell

Debugging Programs and Getting Instructor Help

<<Please refer to this sheet when calling or emailing me for help.>>

Although I am always glad to help any student with any assignment, I ask that you first consider that aside from compiler and operating system bugs, which do infrequently occur, most program failures can be attributed to two things:

  1. Incomplete understanding and, therefore, improper use of data types, operators, statements, or functions:

If your program doesn’t work, do you understand explicitly what every keystroke of code you have written is supposed to do?  Programming is not a “let’s try this” and “let’s try that” discipline.  You must never continue writing code until you fully understand what you have already written.  If you don’t know what an expression is supposed to do, how can you know if it is doing it correctly or not?

The first thing I will ask you when you ask me what’s wrong with your program is to explain it line-at-a-time.  If there are any keystrokes you can’t explain or don’t fully understand you should be asking about them, not the whole program, because therein probably lies your problem.

  1. Careless mistakes

Once you have assured yourself that you understand what every keystroke you have produced is supposed to do you can move on to the next stage of debugging by following these steps:

  1. Choose a test value that causes your program to fail.

  2. On paper only and using that test value, step through your program from the beginning to wherever the program failure is detected.  As you proceed through each step write down the values you think should be in every variable and returned from every function.

  3. If step B above doesn’t point out where the failure occurs, place a printf/cout statement at every point in the program where you wrote down a variable or function return value, such that those values get printed at those points.  Run the program and note where the value actually printed differs from the value you predicted.

  4. Step C above will either make your problem obvious or tell you that you have a basic misunderstanding of the way some data types, operators, statements, or functions work.

Once you have completed the previous four steps and your program still fails, you are ready to contact me via telephone or email (and find out that I probably can’t figure it out either).  Please have both your “paper run” values and your “actual run” values handy so we can discuss them.