Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Input (Read) Statement. Variable Declaration (Definition) Initialization Assignment Displaying variables: using cout statement Reading variables from.

Similar presentations


Presentation on theme: "1 Input (Read) Statement. Variable Declaration (Definition) Initialization Assignment Displaying variables: using cout statement Reading variables from."— Presentation transcript:

1 1 Input (Read) Statement

2 Variable Declaration (Definition) Initialization Assignment Displaying variables: using cout statement Reading variables from the user: using cin statement 2

3 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

4 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 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. - 14 bye

6 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 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 8 Whether the input is 15.50 48.30 only one space or 15.5048.30 tab or 15.50 newline (enter) 48.30 The input statement: cin>>payRate>>hoursWorked; would store 15.50 in payRate and 48.30 in hoursWorked.

9 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 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 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 12 Consider the statement cin>>a; where a is a variable of some simple data type. float

13 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=74.35 4. cin>>z;39z=39.0

14 14 5. cin>>z>>a;65.78 38z=65.78, a=38 6. cin>>a>>b;4 60a=4, b=60 7. cin>>a>>z;57 26.9 a=57, z=26.9 8. cin>>z>>a;57 26.9z=57.0, a=26 9. cin>>a>>z;57 26.9a=57, z=26.9 10. cin>>a>>b>>z;11 34a=11, b=34, Computer waits for the next number

15 15 11. cin>>a>>z;46 32.4 68a=46, z=32.4, 68 is held for later input 12. cin>>a>>z; 35.5 9.5a = 35, z = 0.5

16 16 Example (2) int one, two; float z; 1. one = 4; 2. cin>>two; //8 3. cin>>z; //4.5 4. one = two; ? one ? two ? z 4?? 48? 484.5 88

17 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 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


Download ppt "1 Input (Read) Statement. Variable Declaration (Definition) Initialization Assignment Displaying variables: using cout statement Reading variables from."

Similar presentations


Ads by Google