Chapter 4 INPUT AND OUTPUT OBJECTS

Slides:



Advertisements
Similar presentations
CPS120: Introduction to Computer Science INPUT/OUTPUT.
Advertisements

File streams Chapter , ,
I/O Streams as an Introduction to Objects and Classes
I/O and Program Control Statements Dick Steflik. Overloading C++ provides two types of overloading –function overloading the ability to use the same function.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
CS 117 Spring 2002 Using Files Hanly: Chapter 9, some in Chapter 4 Freidman-Koffman: Chapter 8, iomanip in Chapter 5.
Chapter 3: Input/Output
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 1Winter Quarter I/O Manipulation Lecture.
Unformatted and Formatted I/O Operations. 2 Unformatted Input/output is the most basic form of input/output. Unformatted I/O transfers the internal binary.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
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.
CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,
Output Formatting. Precision #include... float grade = f; cout.precision(4); cout
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Exposure C++ Chapter VII Program Input and Output.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
C++ Streams Lecture-2.
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.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
C++ Streams Lecture-2. C++ Streams Stream  A transfer of information in the form of a sequence of bytes I/O Operations:  Input stream: A stream that.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
CPS120: Introduction to Computer Science Formatted I/O.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 31 Thanks for Lecture Slides: C How to Program by Paul Deital &
Chapter 3: Input/Output
Unit 3 Lesson 6 Input and Output Streams with modifications by Mr. Dave Clausen.
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
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.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
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.
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.
C++ Basic Input and Output (I/O)
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 3 L7.
TMF1414 Introduction to Programming
Chapter 3: Expressions and Interactivity.
Chapter 21 - C++ Stream Input/Output Stream Manipulators
Output Stream Formatting
Basics (Variables, Assignments, I/O)
Chapter 21 - C++ Stream Input/Output
Chapter 2 part #3 C++ Input / Output
Formatting Screen Input/Output
Input and Output Chapter 3.
Standard Input/Output Streams
Standard Input/Output Streams
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
Advanced Input and Output
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.
Chapter 5 Input and Output Streams
Chapter 3: Input/Output
Managing console i/o operations
Chapter 3 Input output.
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Chapter 3: Expressions and Interactivity
Formatted Input, Output & File Input, Output
CHAPTER 2: COMPONENTS OF PROGRAMMING LANGUAGE
Chapter 2 part #3 C++ Input / Output
Presentation transcript:

Chapter 4 INPUT AND OUTPUT OBJECTS

Chapter 4 The cout Object The cout object is used to display information to the monitor and is contained in the iostream.h and iostream header files.

Chapter 4 Using the cout Object cout << item #1 << item #2 << item #3 << ·· << item #n;

Chapter 4 The cout Output Stream Flows to the System Monitor

Chapter 4 Using \n and endl

The C++ Escape Sequences Used with cout Chapter 4 The C++ Escape Sequences Used with cout \a Beep \b Backspace \n CRLF \r CR \t Horizontal tab \v Vertical tab \\ Backslash \' Single quote \” Double quote \? Question mark

Chapter 4 I/O Stream Manipulators setw(n) Sets field width to n setprecision(n) Sets floating-point precision to n setfill(n) Sets fill character to n ws Extracts white space characters endl Inserts a new line in the output stream, then flush Flushes the output stream

Chapter 4 Setting the Field Width cout << setw(field width) << output item;

Chapter 4 Generating Decimal Point Values cout.setf(ios::fixed ); cout.precision(n);

Chapter 4 Generating Left-Justified Values cout.setf(ios::fixed); cout.setf( ios::left);

Chapter 4 Generating Currency Values cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2);

Formatting Functions and Flags setf() Sets a formatting flag. Chapter 4 Formatting Functions and Flags setf() Sets a formatting flag. unsetf() Unsets a formatting flag. precision(n) Sets decimal output to n decimal places. fixed Forces fixed decimal point output. left Forces left justification within field. right Forces right justification within field. scientific Forces exponential, e, output. showpoint Forces a decimal point to be displayed along with trailing 0’s.(Used for currency outputs) showpos Forces a + sign output for positive values.

Chapter 4 The cin Object The cin object is used to read information from the keyboard and is contained in the iostream.h and iostream header files.

Chapter 4 The cin Input Stream Flows from the Keyboard

Using the cin Object cin >> variable to be read; Chapter 4 Using the cin Object cin >> variable to be read; Only one character is read at a time. White space (blanks, tabs, new lines, carriage returns, etc.) are ignored by cin when using the >> operator. However, white space can be read using different cin functions. Numeric values can be read as characters, but each digit is read as a separate character.

Chapter 4 Using get() and put() For Single Character Data cin.get(character variable); cout.put(character variable);

Chapter 4 Reading Strings Using getline() getline(input stream, string object, 'delimiting character’); To avoid a potential problem, use the line cin >> ws before the cin.getline() statement.

Chapter 4 Opening an Input File ifstream fin(”file path"); Opening an Output File ofstream fout(”file path"); Opening an Input/Output File fstream finout(“file path”);

Processing an Open File if(!fileObject) Chapter 4 Processing an Open File if(!fileObject) { cout << "This file cannot be opened" << endl; exit(1); }//END IF else while (Read Data Item From Input File ) { //BEGIN LOOP Process data item. } //END LOOP }//END ELSE