Sunteți pe pagina 1din 32

Completing the Problem-

Solving Process
Objectives
• Get numeric and character data from the keyboard
• Display information on the computer screen
• Write arithmetic expressions
• Type cast a value
• Write an assignment statement
• Code the algorithm into a program
• Desk-check a program
• Evaluate and modify a program
Create a Computer Solution to a Problem
1. Analyze the problem
2. Plan the algorithm
3. Desk-check the algorithm
4. Code the algorithm into a program
5. Desk-check the program
6. Evaluate and modify (if necessary) the program
Getting Data from the Keyboard
Displaying Messages on the Computer Screen
Exercise
Arithmetic Operators in C++
Type Conversions in Arithmetic Expressions
The static_cast Operator
• explicit type conversion or a type cast
Assignment Statements
Step 5—Desk-check the Program
Step 6—Evaluate and Modify the Program
• You evaluate a program by entering your C++ instructions (along with
other instructions that you will learn about later in this section) into
the computer and then using the computer to run (execute) the
program.
• While the program is running, you enter the same sample data used
when desk-checking the program.
• If the results obtained when running the program differ from those
shown in the program’s desk-check table, it indicates that the
program contains errors, referred to as bugs.
• The process of locating and correcting the bugs in a program is called
debugging.
• Program bugs typically are caused by either syntax errors or logic
errors.
• A syntax error occurs when you break one of the programming
language’s rules.
• A logic error can occur for a variety of reasons, such as forgetting to
enter an instruction or entering the instructions in the wrong order.
Process by which source code is translated
into executable code
• The instructions you enter are called source code.
• You save the source code in a file on a disk, giving it the filename
extension .cpp (which stands for C plus plus). The .cpp file is called the
source file.
• A compiler translates high-level instructions into machine code—the
0s and 1s that the computer can understand. Machine code is usually
called object code.
• The linker combines the object file with other machine code
necessary for your C++ program to run correctly, such as machine
code that allows the program to communicate with input and output
devices.
• The linker produces an executable file, which is a file that contains all
of the machine code necessary to run your C++ program as many
times as desired without the need for translating the program again.
• // : comment – internal documentation
• #include <iostream> directive tells the C++ compiler to include the
contents of the iostream file in the current program.
• using namespace std; directive indicates that the definitions of the
standard C++ keywords and classes are located in the std (which
stands for standard) namespace. A namespace is a special area in the
computer’s internal memory.
• main is the name of a function and must be typed using lowercase
letters. A function is a block of code that performs a task.
Exercise
Exercise
Exercise: Correct the errors in the lines of
code shown

S-ar putea să vă placă și