CFP- LESSON 4 PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document is a lesson on C++ programming, focusing on input/output operations. It introduces the cin object for reading user input and demonstrates various aspects of expressions, including mathematical expressions and operators along with combined assignment operators. Basic concepts of type casting are also covered in this lesson.
Full Transcript
Expression and Interactivity 4 TOPICS 4.1 The cin object 4.2 Mathematical Expressions 4.3 Type Casting 4.4 Multiple Assignment and Combined Assignment 4.5...
Expression and Interactivity 4 TOPICS 4.1 The cin object 4.2 Mathematical Expressions 4.3 Type Casting 4.4 Multiple Assignment and Combined Assignment 4.5 Working with Characters and string objects LEARNING OUTCOMES At the end of the lesson, you should be able to: 1. determine the use of cin object; 2. recognize the mathematical expression and its use; 3. learn how to use type casting; 4. learn how to assigned multiple and combined assignment; 5. identify the characters and string objects. 4.1 The cin object IDEA: The cin object can be used to read data typed at the keyboard. So far you have written programs with built-in data. Without giving the user an opportunity to enter his or her own data, you have initialized the variables with the necessary starting values. These types of programs are limited to performing their task with only a single set of starting data. If you decide to change the initial value of any variable, the program must be modified and recompiled. In reality, most programs ask for values that will be assigned to variables. This means the program does not have to be modified if the user wants to run it several times with different sets of data. For example, a program that calculates payroll for a small business might ask the user to enter the name of the employee, the hours worked, and the hourly pay rate. When the paycheck for that employee has been printed, the program could start over again and ask for the name, hours worked, and hourly pay rate of the next employee. Just as cout is C++’s standard output object, cin is the standard input object. It reads input from the console (or keyboard) as shown in Program 4-1. Program 4-1 1 // This program asks the user to enter the length and width of 2 // a rectangle. It calculates the rectangle's area and displays 3 // the value on the screen. 4 #include 5 using namespace std; 6 7 int main() 8 { 9 int length, width, area; 10 11 cout width; 17 area = length * width; 18 cout symbol is the stream extraction operator. It gets characters from the stream object on its left and stores them in the variable whose name appears on its right. In this line, characters are taken from the cin object (which gets them from the keyboard) and are stored in the length variable. Gathering input from the user is normally a two-step process: 1. Use the cout object to display a prompt on the screen. 2. Use the cin object to read a value from the keyboard. The prompt should ask the user a question, or tell the user to enter a specific value. For example, the code we just examined from Program 4-1 displays the following prompt: What is the length of the rectangle? When the user sees this prompt, he or she knows to enter the rectangle’s length. After the prompt is displayed, the program uses the cin object to read a value from the keyboard and store the value in the length variable. Notice that the > operators appear to point in the Instruction that data is flowing. In a statement that uses the cout object, the > operator always points toward the variable that is receiving the value. This indicates that data is flowing from cin to a variable. cin >> length; cout operators as arrows that point in the Instruction that data is flowing. cin ïƒ length; cout  "What is the length of the rectangle? "; The cin object causes a program to wait until data is typed at the keyboard and the [Enter] key is pressed. No other lines in the program will be executed until cin gets its input. cin automatically converts the data read from the keyboard to the data type of the variable used to store it. If the user types 10, it is read as the characters ‘1’ and ‘0’. cin is smart enough to know this will have to be converted to an int value before it is stored in the length variable. cin is also smart enough to know a value like 10.7 cannot be stored in an integer variable. If the user enters a floating-point value for an integer variable, cin will not read the part of the number after the decimal point. NOTE: You must include the iostream file in any program that uses cin. ENTERING MULTIPLE VALUES The cin object may be used to gather multiple values at once. Look at Program 4-2, which is a modified version of Program 4-1. Line 15 waits for the user to enter two values. The first is assigned to length and the second to width. cin >> length >> width; Program 4-2 1 // This program asks the user to enter the length and width of 2 // a rectangle. It calculates the rectangle's area and displays 3 // the value on the screen. 4 #include 5 using namespace std; 6 7 int main() 8 { 9 int length, width, area; 10 11 cout width; 16 area = length * width; 17 cout fractional >> letter; 14 cout