Elementary Programming in C++: Common Errors

Common elementary programming errors often involve undeclared variables, uninitialized variables, integer overflow, unintended integer division, and round-off errors.

Common Error 1: Undeclared/Uninitialized Variables and Unused Variables

A variable must be declared with a type and assigned a value before using it. A common error is not declaring a variable or initializing a variable. Consider the following code:

double interestRate = 0.05;

double interest = interestrate * 45;

This code is wrong, because interestRate is assigned a value 0.05, but interestrate has not been declared and initialized. C++ is case-sensitive, so it considers interestRate and interestrate to be two different variables.

If a variable is declared, but not used in the program, it might be a potential programming error. So, you should remove the unused variable from your program. For example, in the fol­lowing code, taxRate is never used. Therefore, it should be removed from the code.

double interestRate = 0.05;

double taxRate = 0.05;

double interest = interestRate * 45;

cout << “Interest is ” << interest << endl;

Common Error 2: Integer Overflow

Numbers are stored with a limited number of digits. When a variable is assigned a value that is too large (in size) to be stored, it causes overflow. For example, executing the following statement causes overflow, because the largest value that can be stored in a variable of the short type is 32767. 32768 is too large.

short value = 32767 + 1; // value will actually become -32768

Likewise, executing the following statement causes overflow because the smallest value that can be stored in a variable of the short type is -32768. The value -32769 is too large to be stored in a short variable.

short value = -32768 – 1; // value will actually become 32767

C++ does not report errors on overflow. Be careful when working with numbers close to the maximum or minimum range of a given type.

When a floating-point number is too small (i.e., too close to zero) to be stored, it causes underflow  underflow. C++ approximates it to zero. So, normally you need not be concerned with underflow.

Common Error 3: Round-off Errors

A round-off error, also called a rounding error, is the difference between the calculated approximation of a number and its exact mathematical value. For example, 1/3 is approxi­mately 0.333 if you keep three decimal places, and is 0.3333333 if you keep seven decimal places. Since the number of digits that can be stored in a variable is limited, round-off errors floating-point approximation are inevitable. Calculations involving floating-point numbers are approximated because these numbers are not stored with complete accuracy. For example,

float a = 1000.43;

float b = 1000.0;

cout << a – b << endl;

displays 0.429993, not 0.43. Integers are stored precisely. Therefore, calculations with inte­gers yield a precise integer result.

Common Error 4: Unintended Integer Division

C++ uses the same divide operator, namely /, to perform both integer and floating-point divi­sion. When two operands are integers, the / operator performs an integer division. The result of the operation is the quotient. The fractional part is truncated. To force two integers to perform a floating-point division, make one of the integers into a floating-point number. For example, the code in (a) displays that average is 1 and the code in (b) displays that average is 1.5.

Common Error 5: Forgetting Header Files

Forgetting to include appropriate header files is a common compile error. The pow function is defined in the cmath header file and the time function is defined in the ctime header file. To use the pow function in your program, your program needs to include the cmath header. To use the time function in your program, your program needs to include the ctime header. For every program that uses the console input and output, you need to include the iostream header.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *