1 Input (Read) Statement
Variable Declaration (Definition) Initialization Assignment Displaying variables: using cout statement Reading variables from the user: using cin statement 2
3 Input (Read) Statement Job: Read one values from the keyboard Header file: iostream.h Syntax of cin together with >> is : cin>>variable; cin>>variable_1>>variable_2. >>variable_n; OR cin>>variable1 >>variable2 >>variablen; Flowchart: Read or Input
Example Suppose miles is a variable of the type float. The statement cin>>miles; causes the computer to get a value of the type float and place it in the memory cell miles. cin and its operator extract the value from the input stream and store it in the variable enter cin works after pressing “enter” key 4
5 Example void main() { int num; waiting for an input cin>>num; cout<<"bye"; getch(); } input 14 then press Enter then bye displayed 1. the screen is empty waiting for an input. 2. The word “bye” is not displayed until the user press enter.so the compiler executes the next statement bye
6 By using more than one variable in cin, more than one value can be read at the same time. Suppose feet and inch are variables of the type int. A statement like cin>>feet>>inch; gets two integers (entered from the keyboard and separated by space) and places them in the memory location feet and inch, respectively. as input: 34 (leave space) 54 (press enter) 3454 (press enter) 34 (press enter) 54 (press enter) 34 feet 54 inch 3454waiting 3454
7 Reading more than one variable You can read both payRate and hoursWorked via a single cin statement by using the following code: cin>>payRate>>hoursWorked;. OR cin>>payRate; cin>>hoursWorked; When scanning for the next input, >> skips all whitespaces. Whitespace characters consist of blanks and certain nonprintable characters, such as tabs and the newline character.
8 Whether the input is only one space or tab or newline (enter) The input statement: cin>>payRate>>hoursWorked; would store in payRate and in hoursWorked.
9 Prompt Lines Ask user to input the required data by displaying a suitable message. cout<<"Please enter a number between 1 and 10 and" <<" press the return key"<<endl; cin>>num; When these two statements execute in the order given, first the cout statement causes the following line of text to appear on the screen: Please enter a number between 1 and 10 and press the return key - After seeing this line, users know that they must enter a number and press the return key.
10 Example: What is wrong in the following program? How can you make the program more usable and readable? /*Program to read two numbers */ #include void main() { int num1; cin>>num1>>num2; cout<<num1<<“\t”<<num2; getch(); } 1.The variable num2 must be declared before being input 2.Enhance the output using a prompt line
11 Example /*Program to read two numbers */ #include void main() { int num1,num2; cout << “Enter two numbers” << “ separated by space:“ ; cin>>num1>>num2; cout<<num1<<“\t”<<num2; getch(); }
12 Consider the statement cin>>a; where a is a variable of some simple data type. float
13 Example (1) int a,b; float z; StatementInput Value Stored in Memory 1. cin>>a;48a=48 2. cin>>a;46.35a=46,.35 is held for later input 3. cin>>z;74.35z= cin>>z;39z=39.0
14 5. cin>>z>>a; z=65.78, a=38 6. cin>>a>>b;4 60a=4, b=60 7. cin>>a>>z; a=57, z= cin>>z>>a; z=57.0, a=26 9. cin>>a>>z; a=57, z= cin>>a>>b>>z;11 34a=11, b=34, Computer waits for the next number
cin>>a>>z; a=46, z=32.4, 68 is held for later input 12. cin>>a>>z; a = 35, z = 0.5
16 Example (2) int one, two; float z; 1. one = 4; 2. cin>>two; //8 3. cin>>z; // one = two; ? one ? two ? z 4?? 48?
17 What about an attempt to read invalid data? What would happen if you tried to input a letter into an int variable? If the input data did not match the corresponding variables, the program would run into problems. Trying to read a letter into an int or float variable would result in an input failure.
18 Consider the following statements int a, b, c; float x; the statement cin>>a>>b; the input is W 54 would result in an input failure because you are trying to input the character 'W' into the int variable a. input failure a b