bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/C++/C++ Errors
C++•C++ Errors

C++ Errors

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind C++ Errors?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Order

Put the learning moves in the order that makes the concept easiest to apply.

Good Habits to Avoid Errors
Common Runtime Errors
Common Compile-Time Errors

Errors

Even experienced C++ developers make mistakes. The key is learning how to spot and fix them!

These pages cover common errors and helpful debugging tips to help you understand what's going wrong and how to fix it.

Common Compile-Time Errors

Compile-time errors are mistakes that prevent your program from compiling.

1) Missing semicolon:

int x = 5

2) Using undeclared variables:

Runnable example

cout << myVar;

3) Mismatched types (e.g. trying to assign a string to an int ):

int x = "Hello";

Common Runtime Errors

Runtime errors occur when the program compiles but crashes or behaves unexpectedly.

1) Dividing by zero:

int a = 10;
int b = 0;
int result = a / b;
// not possible
cout << result;

2) Accessing out-of-bounds array elements:

int numbers[3] = {1, 2, 3};
cout << numbers[8]; // element
does not exist

3) Using deleted memory (dangling pointer):

int* ptr = new int(10);
delete ptr;
cout << *ptr;
// invalid

Good Habits to Avoid Errors

  • Always initialize your variables
  • Use meaningful variable names
  • Keep your code clean and use indentation to stay organized
  • Keep functions short and focused
  • Check if loops or conditions are running as expected
  • Read error messages carefully - they often tell you exactly where the problem is

In the next chapter, you will learn how to debug your code - how to find and fix bugs/errors in your program.

Next

C++ Debugging