Common Programming
Errors
The errors commonly made when first
programming in C++ include the following:
• 1. Omitting the parentheses after main().
• 2. Omitting or incorrectly typing the opening brace, {, that signifies the start of a function body.
• 3. Omitting or incorrectly typing the closing brace, }, that signifies the end of a function.
• 4. Omitting the semicolon at the end of each C++ executable statement.
• 5. Adding a semicolon after the #include <iostream> preprocessor command.
• 6. Misspelling the name of an object or function, such as typing cot instead of cout.
• 7. Forgetting to enclose a string sent to cout
Fundamentals of Programming
• 1. A computer program is a self-contained unit of
instructions and data used to operate a computer to produce a specific result.
• 2. An algorithm is a step-by-step procedure that must
terminate; it describes how a computation or task is to be performed.
• 3. A C++ program consists of one or more modules called functions. One of these functions must be called main().
The main() function identifies the starting point of a C++
program.
The simplest C++ program consists of the single function main() and has this form:
#include <iostream>
using namespace std;
int main() {
program statements in here;
return 0;
}
• 5. All executable C++ statements within a function body must be terminated by a
semicolon.
• 6. Many functions and classes are supplied in a
standard library provided with each C++ compiler.
• One set of classes, used to create input and output capabilities, is defined in the iostream header file.
• 7. The cout object is used to display text or
numerical results. A stream of characters can be
• sent to cout by enclosing the characters in
quotation marks and using the insertion symbol,
• <<, as in the statement cout << “Hello World!”;.
The text in the string is displayed
• onscreen and can include newline escape sequences for controlling the format.
Data Types, Declarations, and Displays
• 1. The four basic types of data C++ recognizes are integer, floating-point, character, and
• Boolean. Each data type is typically stored in a computer by using different amounts of
• memory.
• 2. The cout object can be used to display all C++ data types.
• 3. Every variable in a C++ program must be declared, and the type of value it can store must
• be specified. Declarations in a function can be placed anywhere in the function, although a
• variable can be used only after it’s declared. Variables can also be initialized when they’re
• declared. Additionally, variables of the same type can be declared with a single declaration
• statement. Variable declaration statements have this general form:
• dataType variableName(s);
4. A simple C++ program containing declaration statements has this typical form:
#include <iostream>
using namespace std;
int main() {
declaration statements;
other statements;
return 0;
}
• 5. Declaration statements always play the software role of informing the
compiler of a function’s valid variable names. When a variable declaration also causes the computer to set aside memory locations for the variable, the
declaration statement is called a definition statement.
• (All declarations used in this chapter have also been definition statements.)
• 6. The sizeof() operator can be used to determine the amount of storage reserved for variables.
Common Programming Errors
• 1. Forgetting to declare all variables used in a program. The compiler detects this error, and an error message is generated for all undeclared variables.
• 2. Attempting to store one data type in a variable declared for a different type. The compiler doesn’t detect this error. The value is converted to the data type of the variable it’s assigned to.
• 3. Using a variable in an expression before a value has been assigned to the variable. Whatever value happens to be in the variable is used when the
expression is evaluated and, therefore, the result of the expression is meaningless.
Assignment and Interactive Input
• 1. An expression is a sequence of one or more operands separated by
operators. An operand is a constant, a variable, or another expression. A value is associated with an expression.
• 2. Expressions are evaluated according to the precedence and associativity of the operators used in the expression.
• 3. The assignment operator is the = symbol. Expressions using this operator assign a value to a variable, and the expression also takes on a value.
Because assignment is a C++ operation, the assignment operator can be used more than once in the same expression.
• 4. The increment operator, ++, adds 1 to a variable, and the decrement operator, --, subtracts 1 from a variable. Both operators can be used as prefixes or postfixes.
• In a prefix operation, the variable is incremented (or
decremented) before its value is used. In a postfix operation,
• the variable is incremented (or decremented) after its value is used.
• 9. The cin object is used for data input. It accepts a stream of data from the keyboard and
• assigns the data to variables. This is the general form of a statement using cin:
• cin >> var1 >> var2 . . . >> varn;
• 10. When a cin statement is encountered, the computer temporarily suspends further execution
• until enough data has been entered for the number of variables in the cin statement.
• 11. It’s a good programming practice to display a message before a cin statement that alerts
• users to the type and number of data items to be entered. This message is called a prompt.
Errors, testing an Debugging
• Program errors can be detected at any of the following times:
• • Before a program is compiled
• • While the program is being compiled
• • While the program is running
• • After the program has been run and the output is being examined
Compile-Time and Runtime Errors
• Errors detected while a program is being compiled are called compile-time errors, and errors that
occur while a program is running are called runtime errors.
• These terms describe when errors occur, not what caused them. Most compile-time errors, however, are caused by syntax errors, and the majority of runtime errors are caused by logic errors.
Syntax and Logic Errors
• Computer literature distinguishes between two main types of errors: syntax and logic errors.
• A syntax error is an error in ordering valid
language elements in a statement or the attempt to use invalid language elements.
cout << “There are four syntax errors here\n cot “ Can you find tem”;
a logic error
• Another error the compiler doesn’t catch is a logic error, which is characterized by erroneous, unexpected, or
unintentional output that’s a result of some flaw in the program’s logic.
• These errors can be detected by desk checking, by program testing, by accident when a user gets erroneous output
while the program is running, or not at all.
Logic Errors
• First, the program executes to completion but
produces incorrect results, such as the following:
• • No output
• • Unappealing or misaligned output
• • Incorrect numerical results