Beginner C++ Mistakes

There are many pitfalls the C++ developer can encounter, especially as this is a more complex and often unforgiving language to master. Beginners need to take C++ a step at a time and digest what they’ve learned before moving on.

Admittedly it’s not just C++ beginners that make the kinds of errors we outline on these pages, even hardened coders are prone to the odd mishap here and there. Here are some common issues to try and avoid.

1. Undeclared Identifiers

A common C++ mistake, and to be honest a common mistake with most programming languages, is when you try and output a variable that doesn’t exist. Displaying the value of x on-screen is fine but not if you haven’t told the compiler what the value of x is to begin with.

2. STD Namespace

Referencing the Standard Library is common for beginners throughout their code, but if you miss the std:: element of a statement, your code errors out when compiling. You can combat this by adding:

using namespace std;

Under the #include part and simply using cout, cin and so on from then on.

3. Semicolons

Remember that each line of a C++ program must end with a semicolon. If it doesn’t then the compiler treats the line with the missing semicolon as the same line with the next semicolon on. This creates all manner of problems when trying to compile, so don’t forget those semicolons.

4. GCC or G++

If you’re compiling in Linux then you will no doubt come across gcc and g++. In short,gcc is the Gnu Compiler Collection (or Gnu C Compiler as it used to be called) and g++ is the Gnu ++ (the C++ version) of the compiler. If you’re compiling C++ then you need to use g++, as the incorrect compiler drivers will be used.

5. No Comments

Indeed the mistake of never making any comments on code is back once more. As we’ve previously bemoaned, the lack of readable identifiers throughout the code makes it very difficult to look back at how it worked, for both you and someone else. Use more comments!

6. Too Many Braces

The braces, or curly brackets, are beginning and ending markers around blocks of code. So for every { you must have a }. Often it’s easy to include or miss out one or the other facing brace when writing code; usually when writing in a text editor, as an IDE adds them for you.

7. Missing Quotes

Missing quotes is a common mistake to make, for every level of user. Remember that quotes need to encase strings and anything that’s going to be outputted to the screen or into a file, for example. Most compilers errors are due to missing quotes in the code.

8. Initialise Variables

In C++ variables aren’t initialised to zero by default. This means if you create a variable called x then, potentially, it is given a random number from 0 to 18,446,744,073,709,551,616, which can be difficult to include in an equation. When creating a variable, give it the value of zero to begin with: x=0.

9. Extra Semicolons

While it’s necessary to have a semicolon at the end of every C++ line, there are some exceptions to the rule. Semicolons need to be at the end of every complete statement but some lines of code aren’t complete statements. Such as:

if lines
switch lines

If it sounds confusing don’t worry, the compiler lets you know where you went wrong.

10. Remember to A.Out

A common mistake when compiling in Linux is forgetting to name your C++ code post compiling. When you compile from the Terminal, you enter:

g++ code.cpp

This compiles the code in the file code.cpp and create an a.out file that can be executed with ./a.out. However, if you already have code in a.out then it’s overwritten. Use:

G++ code.cpp -o nameofprogram

Find more guides like this in…

David Hayward

David has spent most of his life tinkering with technology, from the ZX Spectrum, getting his hands on a Fujitsu VPP5000/100 supercomputer, and coding on an overheating Raspberry Pi. He's written for the likes of Micro Mart, Den of Geek, and countless retro sites and publications, covering reviews, creating code and bench testing the latest tech. He also has a huge collection of cables.

Related Articles

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top button