1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
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.
Chapter 3: Input/Output
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
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 3: Input/Output
Program Input and the Software Design Process ROBERT REAVES.
Basic Elements of C++ Chapter 2.
COMPUTER SCIENCE I C++ INTRODUCTION
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
Due Dates Quiz 1, 2 : Friday September 11 th Quiz 3 : Friday September 18 th Quiz 4 : Monday September 28 th Lab 1, 2 and 3 : Friday Sep 11 th Project.
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.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 3: Input/Output.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
Week 1 Algorithmization and Programming Languages.
1 CS161 Introduction to Computer Science Topic #13.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
Lecture #5: iostream functions دوال الإدخال والإخراج Dr. Hmood Al-Dossari King Saud University Department of Computer Science 4 March 2012.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
1 Today’s Objectives  Announcements Turn in Homework 4 Quiz 4 will be on Wednesday, July 19 – It will have questions about inheritance, polymorphism,
Chapter 3: Input/Output
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
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.
Program Input and the Software Design Process ROBERT REAVES.
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.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
1 Input (Read) Statement. Variable Declaration (Definition) Initialization Assignment Displaying variables: using cout statement Reading variables from.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
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.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1: Introduction to computers and C++ Programming
Basic Elements of C++.
BASIC ELEMENTS OF A COMPUTER PROGRAM
EGR 2261 Unit 3 Input/Output Read Malik, Chapter 3.
Chapter 3: Expressions and Interactivity.
Basic Input and Output Operations
Chapter 2 part #3 C++ Input / Output
Basic Elements of C++ Chapter 2.
Input and Output Chapter 3.
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
Programming with Data Files
Chapter 3: Input/Output
Chapter 3 Input output.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
Chapter 6: User-Defined Functions I
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Chapter 2 part #3 C++ Input / Output
Chapter 1 c++ structure C++ Input / Output
Programming Fundamental-1
Presentation transcript:

1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006

2 Contents What is a stream? (input and output streams) Read data from the standard input device Use predefined functions in a program Use the input stream functions get, ignore, fill, putback, and peek Input failure

3 Input/Output Streams I/O: sequence of bytes (stream of bytes) from source to destination Bytes are usually characters, unless program requires other types of information Stream: sequence of characters from source to destination Input Stream: sequence of characters from an input device to the computer Output Stream: sequence of characters from the computer to an output device

4 Standard I/O Devices Use iostream to extract (receive) data from keyboard and send output to the screen iostream contains definitions of two types istream - input stream ostream - output stream iostream has two variables cin - stands for common input cout - stands for common output

5 Using cin and cout To use cin and cout, the preprocessor directive #include must be used The declaration is similar to the following C++ statements: istream cin; ostream cout; Input stream variables: type istream Output stream variables: type ostream

6 cin and the Extraction Operator >> The syntax of an input statement using cin and the extraction operator >> is cin>>variable>>variable...; The extraction operator >> is binary The left-hand operand is an input stream variable such as cin The right-hand operand is a variable of a simple data type

7 Standard Input Every occurrence of >> extracts the next data item from the input stream Two variables can be read using a single cin statement No difference between a single cin with multiple variables and multiple cin statements with one variable When scanning, >> skips all whitespace Whitespace characters consist of blanks and certain nonprintable characters

8 Data Type of Input >> distinguishes between character 2 and number 2 by the right hand operand of >> If it is of type char, the 2 is treated as character 2 If it is of the type int (or double) the 2 is treated as the number 2

9 Reading Data When reading data into a char variable Extraction operator >> skips leading whitespace, finds and stores only the next character Reading stops after a single character

10 Reading Data (Continued) To read data into an int or double variable Extraction operator >> skips leading whitespace, reads plus or minus sign (if any), reads the digits (including decimal) Reading stops on whitespace non-digit character

11 Using Predefined Functions A function (subprogram): set of instructions When activated, it accomplishes a task main executes when a program is run Other functions execute only when called C++ includes a wealth of functions Predefined functions are organized as a collection of libraries called header files

12 Predefined Functions Header file may contain several functions To use a predefined function, you need the name of the appropriate header file You also need to know: Function name Number of parameters required Type of each parameter What the function is going to do

13 Predefined Function Example To use pow (power), include cmath pow has two numeric parameters The syntax is: pow(x,y) = x y x and y are the arguments or parameters In pow(2,3), the parameters are 2 and 3

14 cin and the get Function The get function Inputs next character (including whitespace) Stores character location indicated by its argument The syntax of cin and the get function: cin.get(varChar); varChar Is a char variable Is the argument (parameter) of the function

15 cin and the ignore Function ignore: discards a portion of the input The syntax to use the function ignore is: cin.ignore(intExp,chExp); intExp is an integer expression chExp is a char expression If intExp is a value m, the statement says to ignore the next m characters or all characters until the character specified by chExp

16 putback and peek Functions Putback function Places previous character extracted by the get function from an input stream back to that stream Peek function Returns next character from the input stream Does not remove the character from that stream

17 putback and peek Functions (continued) The syntax for putback: istreamVar.putback(ch); istreamVar - an input stream variable, such as cin ch is a char variable The syntax for peek: ch = istreamVar.peek(); istreamVar is an input stream variable (cin) ch is a char variable

18 Dot Notation In the statement cin.get(ch); cin and get are two separate identifiers separated by a dot Dot separates the input stream variable name from the member, or function, name In C++, dot is the member access operator

19 Input Failure Things can go wrong during execution If input data does not match the corresponding variables, the program may run into problems Trying to read a letter into an int or double variable would result in an input failure If an error occurs when reading data Input stream enters the fail state

20 Input Failure (continued) Once in a fail state, all further I/O statements using that stream are ignored The program continues to execute with whatever values are stored in variables This causes incorrect results The clear function restores input stream to a working state istreamVar.clear();

21 End of lecture 5 Thank you!