• No results found

Common Programming Errors

N/A
N/A
Protected

Academic year: 2020

Share "Common Programming Errors"

Copied!
21
0
0

Loading.... (view fulltext now)

Full text

(1)

Common Programming

Errors

(2)

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

(3)

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.

(4)

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)

• 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.

(6)

• 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.

(7)

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.

(8)

• 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);

(9)

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;

}

(10)

• 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.

(11)

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.

(12)

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.

(13)

• 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.

(14)

• 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;

(15)

• 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.

(16)

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

(17)

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.

(18)

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”;

(19)

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.

(20)

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

(21)

•Second, a logic error can cause a

runtime error. Examples of this type of logic error are attempts to divide by

zero or take the square root of a

negative number.

References

Related documents

   Recently screening tools useful for the identification of school aged children who have high functioning autism or Asperger’s Disorder have been developed. Can

The present study tested whether a basic feature of the retinotopic organization (upper vs. lower visual field organization) emerges in early visual cortex in sight recovery

The lower rates should encourage saving and invest- ment, and the more neutral treatment of different forms of capital income should reduce distortions and

ผูเขารวมโครงการ จํานวนเปาหมาย 30 คน ไดแก บุคากรสํานักวิทยบริการและเทคโนโลยีสารสนเทศ ศูนยคอมพิวเตอร สํานักวิทยบริการและเทคโนโลยีสารสนเทศ อุทยานการเรียนรูยะลา

The Biotechnology and Biological Sciences Research Council-funded project ‘The validation of biomarkers of metabolic efficacy in infant nutrition’ aims to identify lipid biomarkers

Joe Damiani, Manager of Engineering for the Regional Transportation Commission of Southern Nevada (RTC), detailed that this item entailed a new group of revisions to the

We estimate a log-linear employment equation (i.e., a constant-output demand for labor equation) in which employment is determined by its previous value, current

I authorize any physician or other medical practitioner, any hospital, clinic, or other health-related facility, any medical testing laboratory, any insurer, any state motor