Lesson 6 +10 XP

A Few Common C++ Problems

A Few Common C++ Problems

Every beginner hits the same handful of walls. Here's how to recognize and fix them.

Problem 1: The window closes instantly

You run your program and the window flashes for half a second, great.

That's actually a success - the program finished very quickly and the window closed. A windows app running in an IDE quickly vanishes.

Fix:

  • Run the program from a terminal (the terminal stays open).
  • Or finish with a small pause (e.g. a cin >> x; or a similar stop) that waits for Enter before the window closes.

Problem 2: Missing semicolon

Statements in C++ end with a ;. Forgot one and the compiler complains, often with an "expected ;" message at a line.

Every statement ends with a semicolon.

Problem 3: Case sensitivity

C++ is case-sensitive: main, Main, and MAIN are three completely different words. If you write Main() the linker can't find the entry point and reports "undefined reference to main".

Always spell main with a lowercase m; that's the only form the compiler recognizes.

Problem 4: Missed words of the library

Typing #include <iostrem> (note the typo!) or misspelling cout will confuse the compiler, and the console says "cout was not declared" or similar.

Read your errors

The compiler points at a file and line number (e.g. main.cpp:5). Start there, fix the first error, and many extra errors vanish with it.

TL;DR

  • The closing window just means your program ran fast - run it in a terminal.
  • A missing semicolon is the most common compiler complaint.
  • C++ is case-sensitive: main, Main, MAIN are different.
  • Read the first error plus its line number before checking anything else.