Docsity

Input and Output in C++: Reading and Writing Data with Streams, Slides of Advanced Computer Programming

Partial preview of the text

Download Input and Output in C++: Reading and Writing Data with Streams and more Slides Advanced Computer Programming in PDF only on Docsity!

Chapter 4

Program Input

and the

Software Design

Process

Chapter 4 Topics

• Input Statements to Read Values into a

Program using >>, and functions get,

ignore, getline

• Prompting for Interactive Input/Output

• Using Data Files for Input and Output

• Object-Oriented Design Principles

• Functional Decomposition Methodology

Header File

Access to a library that defines 3 objects

  • An istream object named cin (keyboard)
  • An ostream object named cout (screen)
  • An ostream object named cerr (screen)

4

5

Giving a Value to a Variable

In your program you can assign(give) a value to the variable by using the assignment operator =

ageOfDog = 12;

or by another method, such as

cout << “How old is your dog?”; cin >> ageOfDog;

Extraction Operator(>>)

  • Variable cin is predefined to denote an input stream from the standard input device(the keyboard)
  • The extraction operator >> called “get from” takes 2 operands; the left operand is a stream expression, such as cin--the right operand is a variable of simple type
  • Operator >> attempts to extract the next item from the input stream and to store its value in the right operand variable

7

Input Statements

SYNTAX

These examples yield the same result.

cin >> length; cin >> width;

cin >> length >> width;

cin >> Variable >> Variable.. .;

8

Extraction Operator >>

>> “skips over” (actually reads but does not

store anywhere) leading white space

characters as it reads your data from the

input stream(either keyboard or disk file)

10

char first;

char middle;

char last;

cin >> first ;

cin >> middle ;

cin >> last ;

NOTE: A file reading marker is left pointing to the newline character after the „C‟ in the input stream

first middle last

At keyboard you type:

A[space]B[space]C[Enter]

first middle last

„A‟ „B‟ „C‟

11

Keyboard and Screen I/O

#include

cin

(of type istream)

cout

(of type ostream)

Keyboard Screen

executing program

input data output data

13

STATEMENTS CONTENTS MARKER
POSITION

int i; 25 A\n char ch; 16.9\n float x; cin >> i; 25 A\n 16.9\n

cin >> ch; 25 A\n 16.9\n

cin >> x; 25 A\n

16.9\n

Another example using >>

i ch x

25 „A‟

i ch x

i ch x

i ch x

25 „A‟ 16.

NOTE: shows the location of the file reading marker

14

char first;

char middle;

char last;

cin.get(first);

cin.get(middle);

cin.get(last);

NOTE: The file reading marker is left pointing to the space after the „B‟ in the input stream

first middle last

At keyboard you type:

A[space]B[space]C[Enter]

first middle last

„A‟ „ ‟ „B‟

16

17

Use function ignore()

to skip characters

The ignore() function is used to skip(read and

discard) characters in the input stream

The call

cin.ignore(howMany, whatChar);

will skip over up to howMany characters or until

whatChar has been read, whichever comes first

Another Example Using cin.ignore()

i ch

i ch

i ch

i ch

16 „A‟

„A‟

„A‟

NOTE: shows the location of the file reading marker STATEMENTS CONTENTS MARKER POSITION int i; A 22 B 16 C 19\n char ch;

cin >> ch; A 22 B 16 C 19\n

cin.ignore(100, „B‟); A 22 B 16 C 19\n

cin >> i; A 22 B 16 C 19\n 19

String Input in C++

Input of a string is possible using the extraction

operator >>

20

Example

string message;

cin >> message;

Cout << message;

However...