Table of Contents

Being able to read and interpret error messages is really important in programming. Especially when just starting off, you will be making a lot of syntax errors until you learn the language better.

Make sure you're reading the error messages that the compiler gives you. Usually it will give you a line number you can check, as well as a message to try to explain why the compiler doesn't understand. The messages can be a bit cryptic sometimes, but you can usually do a search and find forum posts explaining how to fix the error.

Make sure to take your own notes while going through the concept introduction! That way you can refer back to this information more easily!

1. Debugging strategies

coding-strategy.png

I have been programming for over two decades. Beyond learning programming syntax, part of software development is also learning how to approach challenges and how to analyze problems.

Early on, it's important to minimize bugs in your code so you don't have too many things to fix at once. Here are some important strategies I highly recommend you use to minimize coding headaches.

Review 1. Write only a few lines of code and build after those few lines. This way, if you made any syntax errors, you'll be able to see the errors right away and have a smaller amount of code to look through to try to fix. If you write a ton of code all at once instead…

  • ○ You might get a ton of errors, making it overwhelming to try to get things working again!!
  • ○ It will be just fine because it's highly unlikely that you'll make mistakes. C++ is a famously non-finnicky language.

Review 2. Oh no! You wrote your entire program all at once without trying to build along the way. Now when you try to build, there's a TON OF ERRORS! What's the best way of fixing it all?

  • ○ Do a little "please stop being broken" dance.
  • ○ Comment out large chunks of code until things build again. Then, slowly uncomment out small chunks of code and then building to check for errors, fixing the few errors that pop up at a time. Do this until you've uncommented out the entire program.
  • ○ Create a new project and start over from the beginning. Hopefully this time you won't make the same mistakes!

Review 3. When your program builds but something is going wrong in the program how should you go about diagnosing the problem to try to find where it stems from?

  • □ Ask yourself "What am I expecting it to do that it is NOT doing?"
  • □ Erase the entire program and start over.
  • □ Step through the program manually, writing down the variable values as they change.
  • □ Shake your fist at your computer angrily.

2. Error message #1 (name = Rachel;)

nocomp.png

Review 4. While trying to build some code, the following error is generated:

error: use of undeclared identifier 'Rachel'
name = Rachel;
  • a. What does this error describe?
    • ○ The compiler thinks the name variable should be in double-quotes because it is a string.
    • ○ The compiler is expecting there to be a variable named Rachel that it can copy the value of into the name variable.
    • ○ The compiler dislikes Rachel personally and will not allow them to write any C++ code.
  • b. Can you tell what the programmer's original intention was with this line of code?
    • ○ To assign the name "Rachel" to the variable name.
    • ○ To copy the value from the Rachel variable to the name variable.
  • c. The programmer probably wanted Rachel to be a string literal, a value to store into name. They probably didn't intend to create a variable named Rachel. So how could this be fixed?
    • ○ Change the code to

      name = "Rachel";
      
    • ○ Change the code to

      string Rachel;
      name = Rachel;
      

3. Error message #2 (float price = $9.99;)

nocomp.png

Review 5. While trying to build some code, the following error is generated:

main.cpp:7:13: error: expected ';' after expression
  float price = $9.99;
      ^
      ;
main.cpp:7:11: error: use of undeclared identifier '$9'
  float price = $9.99;

These errors don't point to the actual issue with the code, but the compiler doesn't know how to read what has been written. Let's look at the errors and try to track down the issue…

  • a. What is the first error saying, from the compiler's point of view?
    • ○ The compiler doesn't see the ; at the end of the line.
    • ○ The compiler is expecting a ; after the data type.
  • b. What is the second error saying, from the compiler's point of view?
    • ○ The $9 should be in double quotes.
    • ○ No variable named $9 has been declared at the time of usage.
    • ○ The $ should be in single quotes.
  • c. The error messages pop up at the line that is causing a problem, but the compiler literally cannot read what we wrote, so it's giving us errors that it sees, but aren't useful to us. Can you tell which part of the code is causing an error?
    • ○ Float values can't have .
    • ○ Floats must be set with cin
    • ○ Float values can't have $
  • d. What is the proper way to assign a value of 9.99 to the price variable?
    • ○ price = "$9.99";
    • ○ price = '$' + 9.99;
    • ○ price = 9.99;

4. Error message #3 (adding a and b)

nocomp.png

Review 6. While trying to build some code, the following error is generated:

main.cpp:14:3: error: use of undeclared identifier 'c'
  c = a + b;
  ^
main.cpp:15:25: error: use of undeclared identifier 'c'
  cout << "Result: " << c << endl;

And the program code looks like this:

#include <iostream>
#include <string>
using namespace std;
int main()
{
  int a, b;

  cout << "Enter a: ";
  cin >> a;

  cout << "Enter b: ";
  cin >> b;

  c = a + b;
  cout << "Result: " << c << endl;

  return 0;
}
  • a. What do the error messages mean?
    • ○ The compiler is expecting "c" to store the sum of "a" and "b", but the data types don't match.
    • ○ The compiler is expecting there to be a variable named "c" but the variable has not been declared.
    • ○ The compiler doesn't allow variables named "c" because "C" is a programming language.
  • b. Can you tell what is wrong with the program?
    • ○ a and b are being input with cin, but c is not. We should add

      cin >> c;
      
    • ○ c is being outputted with the result but it should be "c" instead. We should write:

      cout << "Result: c" << endl;
      
    • ○ a and b are declared as integers, but c is not declared anywhere. We should add

      int c;
      

5. Error message #4 (cin statement)

nocomp.png

Review 7. While trying to build some code, the following error is generated:

error: reference to overloaded function could not
be resolved; did you mean to call it?
cin >> name >> endl;

And the program code is:

#include <iostream>
#include <string>
using namespace std;
int main()
{
  string name;
  cout << "Enter your name: ";
  cin >> name >> endl;

  return 0;
}

Can you tell what is wrong with this line of code?

  • ○ The cin should be a cout instead.
  • ○ endl cannot be used with cin statements.
  • ○ The stream operators should be << instead of >>.

6. Logic errors and basic techniques

Here are some "oldie" ways to track down errors without an actual debugger:

  • cout at each step: If you're trying to figure out where a program is crashing, it can be handy to add a cout statement every few lines of code to track which step it crashed afterwards.
  • cout variable values: It can also be handy to display the value of variables as the program is running to make sure the variables are storing the data you're expecting.
  • adding error checks to your programs: You can add if statements to check for errors, such as making sure you don't divide by 0 before the division occurs.

Review 8. Given this code:

int main()
{
  cout << "How many sandwiches? ";
  float totalSandwiches;
  cin >> totalSandwiches;

  cout << "How many guests? ";
  float totalGuests;
  cin >> totalGuests;

  if ( totalGuests == 0 )
  {
    cout << "ERROR: Cannot divide by 0!" << endl;
    return 1;
  }
  float sandwichesPerPerson = totalSandwiches / totalGuests;

  cout << "Sandwiches per guest: " << sandwichesPerGuest << endl;

  return 0;
}

What kind of diagnosis tool is this?

  • ○ cout variable values
  • ○ Error check
  • ○ cout at each step

7. Additional tips

Review 9. Identify what the following error messages generally imply

  • a. Undeclared identifier
    • ○ Compiler is expecting a variable to be declared, but it is not.
    • ○ There is a semicolon missing at the end of a statement
  • b. Expected ; at end of line
    • ○ Compiler is expecting a variable to be declared, but it is not.
    • ○ There is a semicolon missing at the end of a statement

Review 10. When you encounter a compile error you should…

  • □ Look at the line number referenced in the error
  • □ Scream
  • □ Read the error message
  • □ Search for the error message online if you can't tell why the error is occurring

Review 11. You should always tackle programming assignments by programming the entire thing before doing any building or testing.

○ True / ○ False

Author: RachelWil

Created: 2026-10-14 Wed 11:12

Validate