1 Today’s Objectives Announcements Turn in Homework 4 Quiz 4 will be on Wednesday, July 19 – It...

Post on 17-Jan-2016

213 views 0 download

Tags:

transcript

1

Today’s ObjectivesToday’s Objectives

Announcements• Turn in Homework 4• Quiz 4 will be on Wednesday, July 19 – It will have questions about

inheritance, polymorphism, templates, and stream I/O• Homework 5 is due on next Monday, July 24. Since this date is so close to

the end of the semester, no late assignments will be accepted and email will NOT be accepted!

Stream Input/Output (Ch. 15)• Streams• I/O in C++ with streams• Stream error states• Stream manipulators

Bonus Lab 8 – Debugging with MS Visual Studio

17-Jul-200617-Jul-2006

2

Stream Input/OutputStream Input/Output

Chapter 15

3

StreamsStreams

Stream = a sequence of bytes• Just like a water-filled stream, a C++ stream has a flow,

it can be directed, and it can be stopped. But it cannot flow backwards.

In C++, I/O is accomplished by using streams• A C++ stream is an object – it’s instantiated from a

class, just like any other object• Output – the bytes flow from memory to an output

device• Input – the bytes flow from an input device to memory

Stream I/O (Deitel, 771; Josuttis, 584)Stream I/O (Deitel, 771; Josuttis, 584)

4

I/O in C++I/O in C++

Low-level I/O is unformatted I/O• Specifies the number of bytes to transfer• Example: member functions such as “get”, “put”,

“read”, and “write”

High-level I/O is formatted I/O• Uses bytes in groups corresponding to built-in data

types• Example:int n;cin >> n;

wchar_t = a C++ data type used to store Unicode characters

Stream I/O (Deitel, 771–772)Stream I/O (Deitel, 771–772)

5

Header FilesHeader Files

<iostream>• Basic stream I/O services• Defines objects: cout, cin, cerr, and clog

<iomanip>• Services for formatted I/O with stream

manipulators

<fstream>• Services for file I/O

Stream I/O (Deitel, 772)Stream I/O (Deitel, 772)

6

Stream I/O ClassesStream I/O Classes

Different classes are used to create stream objects for each type of I/O, Fig. 15.1, page 773

istream• Defines input streams• Instantiation of the template class basic_istream<>

using char as the type parameter

ostream• Defines output streams• Instantiation of the template class basic_ostream<>

using char as the type parameter

Stream I/O (Deitel, 772–773; Josuttis, 585)Stream I/O (Deitel, 772–773; Josuttis, 585)

7

Global Stream ObjectsGlobal Stream Objects

cin• Predefined object of the istream class• Used for standard user input, normally connected to the keyboard

cout• Predefined object of the ostream class• Used for standard user output, normally connected to the monitor

cerr• Predefined object of the ostream class• Used for error messages, not buffered, normally connected to the monitor

clog• Predefined object of the ostream class• Used for logging messages, buffered, normally connected to the same

device as cerr

Stream I/O (Deitel, 772–773; Josuttis, 585)Stream I/O (Deitel, 772–773; Josuttis, 585)

8

Stream OutputStream Output

operator<<• Stream insertion operator• Overloaded for output to ostream objects• It’s typesafe because it’s overloaded for all the built-in

data types, and it can be overloaded for our own classes

Member function put• Member function of the ostream class• Used to output single chars• Examples

cout.put( 'A' );cout.put( 65 ); //Using the ASCII value

Stream I/O (Deitel, 775–776; Josuttis, 586)Stream I/O (Deitel, 775–776; Josuttis, 586)

9

Stream InputStream Input

operator>>• Stream extraction operator• Overloaded for input from istream objects• It’s typesafe because it’s overloaded for all the built-in

data types, and it can be overloaded for our own classes

• Ignores whitespace and leaves it in the stream

Member functions of the istream classget, getlineignoreeof, gcountputback, peek

Stream I/O (Deitel, 776–780; Josuttis, 586)Stream I/O (Deitel, 776–780; Josuttis, 586)

10

Using get()Using get()

Used with no arguments, it inputs one charchar c = cin.get();

Can be used with three arguments1. char array2. size limit3. delimiter char (optional: default is ‘\n’)

char buffer[1024];cin.get(buffer,1024,'\n');

Stream I/O (Deitel, 776–780)Stream I/O (Deitel, 776–780)

11

Using get()Using get()

Used with no arguments, it inputs one charchar c = cin.get();

Can be used with three arguments1. char array2. size limit3. delimiter char (optional: default is ‘\n’)

char buffer[1024];cin.get(buffer,1024,'\n');

buffer

\nHe l l o

cin

Stream I/O (Deitel, 776–780)Stream I/O (Deitel, 776–780)

12

Using get()Using get()

Used with no arguments, it inputs one charchar c = cin.get();

Can be used with three arguments1. char array2. size limit3. delimiter char (optional: default is ‘\n’)

char buffer[1024];cin.get(buffer,1024,'\n');

buffer H

el l o\n

cin

Stream I/O (Deitel, 776–780)Stream I/O (Deitel, 776–780)

13

Using get()Using get()

Used with no arguments, it inputs one charchar c = cin.get();

Can be used with three arguments1. char array2. size limit3. delimiter char (optional: default is ‘\n’)

char buffer[1024];cin.get(buffer,1024,'\n');

buffer H

ll o \n

e

cin

Stream I/O (Deitel, 776–780)Stream I/O (Deitel, 776–780)

14

Using get()Using get()

Used with no arguments, it inputs one charchar c = cin.get();

Can be used with three arguments1. char array2. size limit3. delimiter char (optional: default is ‘\n’)

char buffer[1024];cin.get(buffer,1024,'\n');

buffer H

lo\n

e l

cin

Stream I/O (Deitel, 776–780)Stream I/O (Deitel, 776–780)

15

Using get()Using get()

Used with no arguments, it inputs one charchar c = cin.get();

Can be used with three arguments1. char array2. size limit3. delimiter char (optional: default is ‘\n’)

char buffer[1024];cin.get(buffer,1024,'\n');

buffer H

o\n

e l l

cin

Stream I/O (Deitel, 776–780)Stream I/O (Deitel, 776–780)

16

Using get()Using get()

Used with no arguments, it inputs one charchar c = cin.get();

Can be used with three arguments1. char array2. size limit3. delimiter char (optional: default is ‘\n’)

char buffer[1024];cin.get(buffer,1024,'\n');

buffer H

\n

e l l o

Leaves the delimiter in the stream

cin

Stream I/O (Deitel, 776–780)Stream I/O (Deitel, 776–780)

17

Using get()Using get()

Used with no arguments, it inputs one charchar c = cin.get();

Can be used with three arguments1. char array2. size limit3. delimiter char (optional: default is ‘\n’)

char buffer[1024];cin.get(buffer,1024,'\n');

buffer

cin

H

\n

e l l o \0Inserts ‘\0’ terminator

Stream I/O (Deitel, 776–780)Stream I/O (Deitel, 776–780)

18

Using getline()Using getline()

Used with three arguments1. char array2. size limit3. delimiter char (optional: default is ‘\n’)

char buffer[1024];cin.getline(buffer,1024);

Stream I/O (Deitel, 776–780)Stream I/O (Deitel, 776–780)

19

Using getline()Using getline()

Used with three arguments1. char array2. size limit3. delimiter char (optional: default is ‘\n’)

char buffer[1024];cin.getline(buffer,1024);

buffer

\nHe l l o

cin

Stream I/O (Deitel, 776–780)Stream I/O (Deitel, 776–780)

20

Using getline()Using getline()

Used with three arguments1. char array2. size limit3. delimiter char (optional: default is ‘\n’)

char buffer[1024];cin.getline(buffer,1024);

buffer H

el l o\n

cin

Stream I/O (Deitel, 776–780)Stream I/O (Deitel, 776–780)

21

Using getline()Using getline()

Used with three arguments1. char array2. size limit3. delimiter char (optional: default is ‘\n’)

char buffer[1024];cin.getline(buffer,1024);

buffer H

ll o \n

e

cin

Stream I/O (Deitel, 776–780)Stream I/O (Deitel, 776–780)

22

Using getline()Using getline()

Used with three arguments1. char array2. size limit3. delimiter char (optional: default is ‘\n’)

char buffer[1024];cin.getline(buffer,1024);

buffer H

lo\n

e l

cin

Stream I/O (Deitel, 776–780)Stream I/O (Deitel, 776–780)

23

Using getline()Using getline()

Used with three arguments1. char array2. size limit3. delimiter char (optional: default is ‘\n’)

char buffer[1024];cin.getline(buffer,1024);

buffer H

o\n

e l l

cin

Stream I/O (Deitel, 776–780)Stream I/O (Deitel, 776–780)

24

Using getline()Using getline()

Used with three arguments1. char array2. size limit3. delimiter char (optional: default is ‘\n’)

char buffer[1024];cin.getline(buffer,1024);

buffer H

\n

e l l o

Discards the delimiter in the stream

cin

Stream I/O (Deitel, 776–780)Stream I/O (Deitel, 776–780)

25

Using getline()Using getline()

Used with three arguments1. char array2. size limit3. delimiter char (optional: default is ‘\n’)

char buffer[1024];cin.getline(buffer,1024);

buffer H e l l o \0Inserts ‘\0’ terminator

cin

Stream I/O (Deitel, 776–780)Stream I/O (Deitel, 776–780)

26

Using read() and write()Using read() and write()

Input• Member function read of the istream class• Inputs bytes into a char array

Output• Member function write of the ostream class• Outputs bytes in a char array

Examplechar buffer[1024];cin.read( buffer, 5 );cout.write( buffer, 5 );

Stream I/O (Deitel, 776–780)Stream I/O (Deitel, 776–780)

27

Stream Error StatesStream Error States

A data member of the stream objects identifies whether I/O was successful with the following values

goodbit = everything okay, normally set to 0

eofbit = end-of-file encountered

failbit = operation not successful, but the stream is okay, e.g. char in the stream instead of an integer

badbit = operation not successful, because stream is corrupted or lost, e.g. reading past the end of a file

Stream I/O (Deitel, 766–768; Josuttis 597)Stream I/O (Deitel, 766–768; Josuttis 597)

28

Determining the Stream StateDetermining the Stream State

Use the following member functions to determine the state of the flags

good() = returns true if the stream is okay

eof() = returns true if the end-of-file was encountered

fail() = returns true if there was an I/O error

operator! = same as fail()

bad() = returns true if there was a fatal error

clear() = clears all flags

Stream I/O (Deitel, 797–799; Josuttis 597)Stream I/O (Deitel, 797–799; Josuttis 597)

29

Example: Checking Stream StateExample: Checking Stream State

int main(){ int n = 1; char c = 'x';

while( n > 0 ){cout << "Enter an integer: ";cin >> n;if( !cin ){ //check the failbit

cout << "That was not an integer!\n";cin.clear(); //clear the flagscin >> c; //something is still in the streamcout << "You entered " << c << '\n';

}else cout << "You entered " << n << '\n';

}}

Stream I/OStream I/O

30

Stream ManipulatorsStream Manipulators

Special objects used to format a stream Used with the stream insertion operator or stream

extraction operator

Stream I/O (Deitel, 781–796; Josuttis, 586)Stream I/O (Deitel, 781–796; Josuttis, 586)

cout << setw(10) << right << "Hello" << endl;

Manipulators that do not require arguments will not need an extra header file.

Manipulators that require arguments will need the <iomanip> header file.

31

Some Stream ManipulatorsSome Stream Manipulators

Manipulator Meaning

setw() Set the width of the output

left Left-justify the output

right Right-justify the output

setprecision() Set the number of digits to the right of the decimal point in floating point numbers

showpoint Specifying that floating point numbers are always output with a decimal point

fixed Use fixed-point notation for the output format of floating point numbers

scientific Use scientific notation for the output of floating point numbers

uppercase Set the ‘E’ in scientific notation to uppercase

Stream I/O (Deitel, 781–796; Josuttis, 586)Stream I/O (Deitel, 781–796; Josuttis, 586)

32

Member Functions for FormattingMember Functions for Formatting

Streams have member functions that can be used to set their format

• Can be used in combination with stream manipulators• Some member functions do the same thing as the manipulators

Examples:

width(n) • Sets the width of the output to n• Equivalent to the “setw(n)” stream manipulator• cout.width(10);

precision(n) • Sets the width of the output to n• Equivalent to the “setprecision(n)” stream manipulator• cout.precision(2);

Stream I/O (Deitel, 781–796; Josuttis, 586)Stream I/O (Deitel, 781–796; Josuttis, 586)

33

Bonus Lab 8Bonus Lab 8

Debugging with MS Visual Studio

Ford and Teorey, Ch. 5

34

Debugging MethodsDebugging Methods

Semantic errors• Logic errors in your code• Examples: code that creates an infinite loop, off-by-

one error

Interactive debugger• Can run your program line by line• You can stop program execution at any point and

examine the values of its variables

Debugging (Ford)Debugging (Ford)

35

Using the MS Visual StudioInteractive Debugger

Using the MS Visual StudioInteractive Debugger

Use “Debug” mode Step Into

• Executes the next statement• Will start at the first line in main if there’s no breakpoint• If the next statement is a function call, it will enter the function

The yellow arrow• Points to the next statement that will be executed

Step Over• Executes the next statement• If the next statement is a function call, it will skip over the function

Step Out• Will finish execution of a function and run to the next statement

Breakpoint• The line where you want your program to stop execution• Set by clicking in the margin at the left of the line of code

Variables pane• Displays a list of current variables and their values• Auto, Locals, and Watch tabs

Debugging (Ford)Debugging (Ford)

36

ReferencesReferences

Deitel, H. M., and P. J. Deitel, C++ How to Program, Fifth Edition. Upper Saddle River, NJ: Prentice Hall, 2005.

Ford, A. R., and T. J. Teorey, Practical Debugging in C++. Upper Saddle River, NJ: Prentice Hall, 2002.

Josuttis, Nicolai M., The C++ Standard Library, A Tutorial and Reference. Boston: Addison-Wesley, 1999.