Listed below are the most common mistakes beginners make when writing C++. If when you compile your program you get lots of error messages, or error messages you can't figure out, read your program carefully to see if you have made any of these.
Writing C++ keywords with an initial capital, such as If for if, Else for else, or While for while. Using any of If, Else, While, and others will probably result in "undefined symbol" messages.
Writing any of several words, commonly used in pseudocode, that are not used in C++, such as set, declare, then, and endif. Using these will probably result in "undefined symbol" messages.
Forgetting the semicolon at the end of a statement that does not end in }: Remember that every statement in C++ ends in either ; or } .
Declaring a variable with one capitalization pattern, and then using it with another, such as: int myHeightInInches; myHeightinInches = 63;. This will result in an "undefined symbol" message.
Forgetting the closing double-quote (") at the end of a string. Make sure that your double-quotes come in pairs. Forgetting a matching double-quote can result in any of a variety of error messages, many of which may be misleading, as the compiler is still looking for the missing double-quote!
Forgetting the single-quotes around a character constant, such as writing W when you mean 'W': This usually results in an "undefined symbol" message.
Writing a character constant with double-quotes, or a string constant with single-quotes.
Using "smart" single-quotes around a character constant, instead of "plain vanilla" single quotes: If your character constants look like ‘A’ or ‘A’ instead of 'A', the compiler will not recognize them as a character constants. This problem usually results from using a word processing program (such as Microsoft Word) to write your program, instead of Notepad or the development environment's own text editor. (A similar problem would result from using smart double-quotes around a string constant.)
Using the assignment operator = where the equality comparison operator == is intended: For example, writing if (x = y) doSomething(); where if (x == y) doSomething(); was intended. This error is not detected by the compiler, and can result in truly bizarre behavior of the program.
The inadvertent Do-Nothing statement: A semicolon standing along is a valid statement, and it does nothing. This will normally not cause a compile time error message, but may result in incorrect operation. Consider the example,
int x = 5; while (x != 0);
The format of a while statement is while (condition) statement. In our example, statement is ; and does nothing. So x remains equal to 5 and the while loop loops forever.
Not enough parentheses when using logical operators and comparison operators: A good example of this is writing if (!x < y) {whatever}. If you write this, you almost certainly mean if (!(x < y)) {whatever}. However, C++ holds that ! has higher precedence than <, and interprets what you wrote as if you intended if ((!x) < y) {whatever}. Nonsense, you say! But C (and by extension C++) is a language written by programmers for programmers, and the compiler assumes that you meant exactly what you wrote. So (assuming x and y are ints), the compiler generates code to
which is not at all what you intended.
(This unfortunate behavior was present in the predecessor language C, and was retained in C++ for backward compatibility. It does not occur in Java. In Java, the code int x = 5, y = 6; boolean b = !x < y; results in the error message"! cannot be applied to int.")
Revised 9 March 2007