Input and Output - Trinity College Dublin · istream - input stream!! ostream - output stream!!...

16
1 4 I/O 1 Input and Output (and predefined functions) 1E3 Topic 4 4 I/O 2 Objectives Learn what a stream is and examine input and output streams Explore how to read data from the standard input device Learn how to write data to the standard output device Learn to use predefined functions. Discover how to use manipulators in a program to format output

Transcript of Input and Output - Trinity College Dublin · istream - input stream!! ostream - output stream!!...

Page 1: Input and Output - Trinity College Dublin · istream - input stream!! ostream - output stream!! iostream has two variables!! cin - stands for common input!! cout - stands for common

1

4 I/O 1

Input and Output!(and predefined functions)"

1E3!Topic 4!

4 I/O 2

Objectives"

n  Learn what a stream is and examine input and output streams!

n  Explore how to read data from the standard input device!

n  Learn how to write data to the standard output device!

n  Learn to use predefined functions.!n  Discover how to use manipulators in a

program to format output

Page 2: Input and Output - Trinity College Dublin · istream - input stream!! ostream - output stream!! iostream has two variables!! cin - stands for common input!! cout - stands for common

2

4 I/O 3

Resources"

n  This topic is based on Chapter 3 of the textbook.!

n  You should read Chapter 3 now.!n  Skip Putback & Peek; Input Failure; The Clear

function; and File Input /Output!n  These notes don’t cover everything.!

What  does  this  pseudocode  compute?  

A.  Sum  of  numbers  B.  Max  of  numbers  C.  Min  of  numbers  D.  None  of  the  above  

Input a number-list.!Set x to 0.!FOR EACH number on number-list! IF number > x! THEN set x to number.!Output x.!

Page 3: Input and Output - Trinity College Dublin · istream - input stream!! ostream - output stream!! iostream has two variables!! cin - stands for common input!! cout - stands for common

3

4 I/O 5

Introduction"n  We’ve seen!

n  cin >> n;!n Assuming n is declared as an integer, this reads a

number from the keyboard and stores it in the variable n.!

n  cout << “The answer is “ << sum;!n Writes the string in the “ “ followed by the value

of the variable sum to the screen.!n  Here we look more closely at this kind of

thing.!

4 I/O 6

Input/Output Streams"n  I/O: sequence of bytes (stream of bytes) from

source to destination!n  Bytes are usually characters, unless program

requires other types of information !n  Stream: sequence of characters from source to

destination!n  Input Stream: sequence of characters from an

input device to the computer!n  Output Stream: sequence of characters from the

computer to an output device!

Page 4: Input and Output - Trinity College Dublin · istream - input stream!! ostream - output stream!! iostream has two variables!! cin - stands for common input!! cout - stands for common

4

4 I/O 7

Standard I/O Devices"n  Use iostream to extract (receive) data from

keyboard and send output to the screen!n  iostream contains definitions of two types!

n  istream - input stream!n  ostream - output stream!

n  iostream has two variables!n  cin - stands for common input!n  cout - stands for common output!

4 I/O 8

Using cin and cout"

n  To use cin and cout, the preprocessor directive !#include <iostream> must be used!

n  The declaration is similar to the following C++ statements: !istream cin;

ostream cout;

!

Page 5: Input and Output - Trinity College Dublin · istream - input stream!! ostream - output stream!! iostream has two variables!! cin - stands for common input!! cout - stands for common

5

4 I/O 9

cin and the Extraction Operator >>"

n  The syntax of an input statement using cin and the extraction operator >> is!

! !cin >> variable >> variable...;

n  The extraction operator >> is binary!

n  The left-hand operand is an input stream variable such as cin

n  The right-hand operand is a variable of a simple data type!

4 I/O 10

Standard Input"

n  Every occurrence of >> extracts the next data item from the input stream!n  cin >> diam >> price;!

n  is equivalent to !n  cin >> diam; "

cin >> price;!

n  >> skips all whitespace!n  Whitespace characters consist of blanks

and certain nonprintable characters!

Page 6: Input and Output - Trinity College Dublin · istream - input stream!! ostream - output stream!! iostream has two variables!! cin - stands for common input!! cout - stands for common

6

4 I/O 11

Data Type of Input"n  >> distinguishes between character 2 and

number 2 by the right hand operand of >> n  If it is of type char, the 2 is treated as character 2 !n  If it is of the type int (or double) the 2 is treated as

the number 2

4 I/O 12

Reading Data"

n  When reading data into a char variable!

n  Extraction operator >> skips leading whitespace, finds and stores only the next character!

n  Reading stops after a single character!

Page 7: Input and Output - Trinity College Dublin · istream - input stream!! ostream - output stream!! iostream has two variables!! cin - stands for common input!! cout - stands for common

7

4 I/O 13

Reading Data (Continued)"

n  To read data into an int or double variable !

n  Extraction operator >> skips leading whitespace, reads plus or minus sign (if any), reads the digits (including decimal)!

n  Reading stops on whitespace non-digit character!

n  Study example 3.1 in the text.!

4 I/O 14

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

collection of libraries called header files!

Page 8: Input and Output - Trinity College Dublin · istream - input stream!! ostream - output stream!! iostream has two variables!! cin - stands for common input!! cout - stands for common

8

4 I/O 15

Predefined Functions"n  Header file may contain several functions !n  To use a predefined function, you need the name

of the appropriate header file!n  You also need to know:!

n  Function name!n  Number of parameters required!n  Type of each parameter!n  What the function is going to do!

4 I/O 16

Predefined Function Example"n  To compute xy use pow (power) function.!

n  pow is in the cmath library!

n  As are sqrt, sin, cos, log, log10, …

n  pow takes two numeric parameters!

n  pow returns a number.!

n  E.g. x = pow(2,3);"!cout << pow (x, 3);!

Page 9: Input and Output - Trinity College Dublin · istream - input stream!! ostream - output stream!! iostream has two variables!! cin - stands for common input!! cout - stands for common

9

Use the cmath library

4 I/O 18

Example continued"

n  The output of the previous program is!

2 to the power of 6 = 64

12.5 to the power of 3 = 1953.12

u = 64 The example shows how to call a function,

pass parameters to it and use the value that it returns (if it returns one).!

Page 10: Input and Output - Trinity College Dublin · istream - input stream!! ostream - output stream!! iostream has two variables!! cin - stands for common input!! cout - stands for common

10

4 I/O 19

cin and the get Function"n  >> can’t be used to read blanks or to notice new

lines.!n  because >> skips blanks and newlines.!

n  The get function!n  Inputs next character (including whitespace)!n  Stores character at location indicated by its argument!

n  The syntax of cin and the get function: ! " cin.get(varChar);

!

4 I/O 20

Example"n  Suppose I want to read two characters,

either of which may be a blank, followed by an integer:!cin.get(ch1);!cin.get(ch2);!cin >> num;!

n  If I type “A 34”, !n  ch1 will be ‘A’, ch2 will be ‘ ‘ and num will

be 34.!

Page 11: Input and Output - Trinity College Dublin · istream - input stream!! ostream - output stream!! iostream has two variables!! cin - stands for common input!! cout - stands for common

11

4 I/O 21

Other istream functions"n  Note the sections on ignore, putback

and peek for future reference.!n  These are functions which allow greater

control over the input stream.!n  For example!

n  cin.ignore (100, ‘.’);

n  Skips up to 100 characters until a full stop is read. The ‘.’ would be discarded too.

4 I/O 22

Input Failure"n  Things can go wrong during execution!n  If input data does not match the corresponding

variables, the program may run into problems!n  Trying to read a letter into an int or double

variable would result in an input failure!n  If an error occurs when reading data!

n  Input stream enters the fail state!

Page 12: Input and Output - Trinity College Dublin · istream - input stream!! ostream - output stream!! iostream has two variables!! cin - stands for common input!! cout - stands for common

12

4 I/O 23

Input Failure (continued)"n  Once in a fail state, all further I/O statements

using that stream are ignored !n  The program continues to execute with

whatever values are stored in variables !n  This causes incorrect results!n  The clear function restores input stream to a

working state!! !istreamVar.clear();

Output"

n  The syntax of cout and << is:!!cout<< expression or manipulator << expression or manipulator

<< ...;

n  Called an output (cout) statement!n  The << operator is called the insertion operator

or the stream insertion operator!n  Expression evaluated and its value is printed at

the current cursor position on the screen!

Page 13: Input and Output - Trinity College Dublin · istream - input stream!! ostream - output stream!! iostream has two variables!! cin - stands for common input!! cout - stands for common

13

Output (continued)"n  Manipulator: alters output!

n  endl: the simplest manipulator!

n  Causes cursor to move to beginning of the next line!

n  cout << u << endl << name;!

n  where u=2034 and name is “Mary” would produce!

!2034 Mary!

The New Line Character"n  Another way of moving to a new line is to

include ‘\n’ in an output string.!n  ‘\n’ is the new line character. n  Tells the output to go to the next line!

n  cout << “Line 1\nLine 2\n” << name;"

n  where name is “Mary” would produce "n  Line 1 Line 2 Mary"

Page 14: Input and Output - Trinity College Dublin · istream - input stream!! ostream - output stream!! iostream has two variables!! cin - stands for common input!! cout - stands for common

14

4 I/O 28

Formatting Output"n  As well as the endl manipulator!

n  setprecision(n) outputs decimal numbers with up to n decimal places!

n  fixed outputs floating-point numbers in a fixed decimal format!

n  showpoint forces output to show the decimal point and trailing zeros!

Page 15: Input and Output - Trinity College Dublin · istream - input stream!! ostream - output stream!! iostream has two variables!! cin - stands for common input!! cout - stands for common

15

4 I/O 29

The setw Manipulator"

n  setw outputs the value of an expression in specific columns!

n  If the number of columns exceeds the number of columns required by the expression!n  Output of the expression is right-justified!n  Unused columns to the left are filled with

spaces!

4 I/O 30

Types of Manipulators"n  Two types of manipulators: !

n  With parameters!

n  Without parameters !n  Parameterized: require iomanip header!

n  setprecision, setw, and setfill n  Nonparameterized: require iostream

header!n  endl, fixed, showpoint, left, and flush

Page 16: Input and Output - Trinity College Dublin · istream - input stream!! ostream - output stream!! iostream has two variables!! cin - stands for common input!! cout - stands for common

16

4 I/O 31

I/O and the string Type"

n  An input stream variable (cin) and extraction operator >> can read a string into a variable of the data type string!

n  Extraction operator!n  Skips any leading whitespace characters and

reading stops at a whitespace character !n  Can not be used to read strings with blanks!

n  The function getline n  Reads until end of the current line!n  Should be used to read strings with blanks!

4 I/O 32

Highlights"q cin and cout are the common input and

output streams.!n  >> extracts data from an input stream into

a variable.!n  << pushes data to an output stream!n  get and getline allow finer level control!n  iomanip library provides manipulators for

formatting output.!