A few helpful hints on how to find a problem in your code.

Common Problems

  • In helping with students, one of the most common problems I've encountered is the “=” vs the “==” statements. When comparing two things (exe: while(foo == false) ) make sure you have the “==” sign rather than the “=”. The latter makes an assignment while the first checks a condition.
  • Putting a “;” on the end of a statement.
  • Making sure your file is read.
  • A “;” after if() statements (this is wrong. Don't do this.)
  • Forgetting a “}” at the end of a loop or function.
  • Often, a “subscript out of range” error appears. This usually means that you are trying to access something that isn't there; i.e. trying to access the 6th element in a vector when there are only 5 elements; or trying to do a str.substr() call on a string that has less letters than you are asking for. Check all of these instances in your program before you freak out. :)
  • In the book, on pages XX - XXII there are lists of common errors and their page numbers.

Test Cases

  • Use Test Cases! Visit the Test cases page to find out what kind of test cases will help you.
  1. The point of a test case is to find out where your code breaks: create a situation where your code won't work and then fix it.
  2. Once you fix all of the possible problems found in your test cases, you are ready to pass off.

Finding The Problem

Breakpoints

  1. Breakpoints help you see what each value is at a given point in your code.
  2. Once you understand what each value is, you can determine where your code goes wrong.

Debug Statements

  • Debug or Print Statements:
  1. If for some reason you are not able to figure out where your code goes wrong with breakpoints, print out every value before you do anything with it.

Example:

            string fname;
            string lname;
            int ss_num;
            double in_bal;
 
            in_file >> fname >> lname >> ss_num >> in_bal;
 
            string name = fname + " " + lname; // Making the name be first + last names
 
            // here, I would make sure that my name is correct, so I would:
            cout << "Name at point 1: " << name << endl;
 
            Account *tempAccount = new Account(name, ss_num, in_bal); // allocating memory and creating the acount
 
            // I want to check to see if the values stored in the object are correct:
            cout << "Name of tempAccount: " << tempAccount ->get_name() << endl;
            cout << "Balance of tempAccount: " << tempAccount->get_bal() << endl;
 
            bank.push_back(tempAccount); // Adding the pointer to the vector;
 
            //Here, let's check to see if anything got added to the vector:
            cout << "Vector size after push_back: " << bank.size() << endl;

If the output from running this program prints

Name at point 1: Paul Bodily
and that's all, then the problem happened when I called the constructor. You can put print statements inside functions or class functions in the same way to make find problems in functions or class functions.

If All Else Fails

  • If you have looked at all of these situations and still can't find your problem, Ask a TA for help.
cs-142/debugging_guide.txt · Last modified: 2015/01/07 16:12 by ryancha
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0