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

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
C++ plus. 2 Goals Some general C++ tips 3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Chapter 3: Input/Output
Program Input and the Software Design Process ROBERT REAVES.
Programming is instructing a computer to perform a task for you with the help of a programming language.
CHAPTER 2 BASIC ELEMENTS OF C++. In this chapter, you will:  Become familiar with the basic components of a C++ program, including functions, special.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CHAPTER 3 INPUT/OUTPUT. In this chapter, you will:  Learn what a stream is and examine input and output streams  Explore how to read data from the standard.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Basic Elements of C++.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What.
Chapter 3: Input/Output
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Input/Output. Objectives In this chapter you will: Learn what a stream is and examine input and output streams Explore how to use the input stream functions.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
Chapter 02 (Part II) Introduction to C++ Programming.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Basic Elements of C++
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
CMPT 201.
What Actions Do We Have Part 1
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Programming Fundamental
BASIC ELEMENTS OF A COMPUTER PROGRAM
getline() function with companion ignore()
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
Introduction to C++ Programming
Programming Funamental slides
Programming Funamental slides
Chapter 3: Input/Output
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter 3 Input output.
CS150 Introduction to Computer Science 1
Programs written in C and C++ can run on many different computers
What Actions Do We Have Part 1
Dale/Weems/Headington Program Input and the Software Design Process
CPP Programming Language
getline() function with companion ignore()
Programming Fundamental-1
Presentation transcript:

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