Chapter 3: Input/Output

Slides:



Advertisements
Similar presentations
1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
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.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
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
Basic Elements of C++ Chapter 2.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
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.
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.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Chapter 3 Arithmetic Expressions, Function Calls, and Output
Syntax and Semantics, and the Program Development Process ROBERT REAVES.
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
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.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester 1436 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel.
Chapter 3: Input/Output
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
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 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
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.
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 3 COMPLETING THE PROBLEM- SOLVING PROCESS AND GETTING STARTED WITH C++ An Introduction to Programming with C++ Fifth Edition.
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.
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.
Bill Tucker Austin Community College COSC 1315
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
Chapter Topics The Basics of a C++ Program Data Types
Completing the Problem-Solving Process
CPS120: Introduction to Computer Science
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.
Chapter 2: Basic Elements of C++
Chapter 2 part #3 C++ Input / Output
Basic Elements of C++ Chapter 2.
Input and Output Chapter 3.
Standard Input/Output Streams
Standard Input/Output Streams
Expressions and Interactivity
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Introduction to cout / cin
Chapter 3 Input output.
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
C++ for Engineers and Scientists Second Edition
Chapter 1 c++ structure C++ Input / Output
Programming Fundamental-1
Presentation transcript:

Chapter 3: Input/Output

Outline In this chapter, you will study: I/O Streams and Standard I/O Devices Using Predefined Functions in a Program Input Failure Output and Formatting Output Input / Output and the string Type Debugging: Understanding Logic Errors and Debugging with cout Statements

I/O Streams and Standard I/O Devices 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

I/O Streams and Standard I/O Devices (cont’d.) Use iostream header file to receive data from keyboard and send output to the screen Contains definitions of two data types: istream: input stream ostream: output stream Has two variables: cin: stands for common input cout: stands for common output

I/O Streams and Standard I/O Devices (cont’d.) Variable declaration is similar to: istream cin; ostream cout; To use cin and cout, the preprocessor directive #include <iostream> must be used Input stream variables: type istream Output stream variables: type ostream

cin and the Extraction Operator >> The syntax of an input statement using cin and the extraction operator >> is: The extraction operator >> is binary Left-side operand is an input stream variable Example: cin Right-side operand is a variable

cin and the Extraction Operator >> (cont’d.) No difference between a single cin with multiple variables and multiple cin statements with one variable When scanning, >> skips all whitespace Blanks and certain nonprintable characters >> distinguishes between character 2 and number 2 by the right-side operand of >> If type char or int (or double), the 2 is treated as a character or as a number 2

cin and the Extraction Operator >> (cont’d.) Entering a char value into an int or double variable causes serious errors, called input failure

cin and the Extraction Operator >> (cont’d.) When reading data into a char variable >> skips leading whitespace, finds and stores only the next character Reading stops after a single character To read data into an int or double variable >> skips leading whitespace, reads + or - sign (if any), reads the digits (including decimal) Reading stops on whitespace non-digit character

cin and the Extraction Operator >> (cont’d.)

cin and the Extraction Operator >> (cont’d.)

cin and the Extraction Operator >> (cont’d.)

Using Predefined Functions in a Program 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

Using Predefined Functions in a Program (cont’d.) 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

Using Predefined Functions in a Program (cont’d.) To use pow (power), include cmath Two numeric parameters Syntax: pow(x,y) = xy x and y are the arguments or parameters In pow(2,3), the parameters are 2 and 3

Using Predefined Functions in a Program (cont’d.)

Using Predefined Functions in a Program (cont’d.)

Input Failure Things can go wrong during execution If input data does not match corresponding variables, program may run into problems Trying to read a letter into an int or double variable will result in an input failure If an error occurs when reading data Input stream enters the fail state C++ Programming: From Problem Analysis to Program Design, Sixth Edition

Input Failure (cont’d) C++ Programming: From Problem Analysis to Program Design, Sixth Edition

Input Failure (cont’d) C++ Programming: From Problem Analysis to Program Design, Sixth Edition

The clear Function 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 C++ Programming: From Problem Analysis to Program Design, Sixth Edition

The clear Function (cont’d) C++ Programming: From Problem Analysis to Program Design, Sixth Edition

The clear Function (cont’d) C++ Programming: From Problem Analysis to Program Design, Sixth Edition

Output and Formatting Output Syntax of cout when used with << expression is evaluated value is printed manipulator is used to format the output Example: endl

setw Outputs the value of an expression in a specified number of columns cout << setw(5) << x << endl; If number of columns exceeds the number of columns required by the expression Output of the expression is right-justified Unused columns to the left are filled with spaces Must include the header file iomanip

Input/Output and the string Type An input stream variable (cin) and >> operator can read a string into a variable of the data type string Extraction operator Skips any leading whitespace characters Reading stops at a whitespace character The function getline Reads until end of the current line

Debugging: Understanding Logic Errors and Debugging with cout statements Syntax errors Reported by the compiler Logic errors Typically not caught by the compiler Spot and correct using cout statements Temporarily insert an output statement Correct problem Remove output statement