Chapter 4 Program Input and the Software Design Process

Preview:

DESCRIPTION

Chapter 4 Program Input and the Software Design Process. Dale/Weems. Input Statements to Read Values into a Program using >> Prompting for Interactive Input/Output. C++ Input/Output. No built-in I/O in C++!! A library provides input stream and output stream. Keyboard. Screen. executing - PowerPoint PPT Presentation

Citation preview

1

Chapter 4

Program Input and the

Software Design Process

Dale/Weems

2

Input Statements to Read Values into a Program using >>

Prompting for Interactive Input/Output

C++ Input/Output No built-in I/O in C++!! A library provides input stream and

output stream

Keyboard Screenexecutingprogram

ostreamistream

3

4

Using Libraries A library has 2 parts

Interface(stored in a header file)tells what items are in the library and how to use them

Implementation(stored in another file)contains the definitions of the items in the library

#include <iostream> Refers to the header file for the iostream

library needed for use of cout and endl.

5

Is compilation the first step? No; before your source program is compiled,

it is first examined by the preprocessor that

removes all comments from source code handles all preprocessor directives--they begin

with the # character such as

#include <iostream> This include tells the preprocessor to look in the

standard include directory for the header file called iostream and insert its contents into your source code

<iostream> Header File

Access to a library that defines 2 objects

An istream object named cin (keyboard)

An ostream object named cout (screen)

6

7

Giving a Value to a VariableRECALL: 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;

>> Operator

>> is called the input or extraction operator

>> is a binary operator

>> is left associative

Expression Has value

cin >> age cinStatement

cin >> age >> weight;8

SYNTAX

These examples yield the same result.

cin >> length;

cin >> width;

cin >> length >> width;

Input Statements

cin >> Variable >> Variable . . .;

10

Extraction Operator >>

>> “skips over” (actually reads but does not store anywhere) leading white space characters as it reads your data from the input stream

11

Whitespace Characters Include . . .

blanks tabs end-of-line(newline) characters

The newline character is created by hitting Enter or Return at the keyboard, or by using the manipulator endl or “\n” in a program

12

char first; char middle; char last;

cin >> first ; cin >> middle ; cin >> last ;

NOTE: A reading marker is left pointing to the newline character after the ‘C’ in the input stream

first middle last

At the keyboard you type: A[space]B[space]C[Enter]

first middle last

‘A’ ‘B’ ‘C’

13

25 A\n16.9\n

25 A\n16.9\n

25 A\n16.9\n

25 A\n16.9\n

STATEMENTS CONTENTS MARKER POSITION

int i; char ch; float x;

cin >> i;

cin >> ch;

cin >> x;

Another example using >>

i ch x

25

i ch x

i ch x

i ch x

16.916

25 ‘A’

25 ‘A’

22

Example

string message; cin >> message; cout << message;

However . . .

String Input in C++

Input of a string is possible using the extraction operator >>

23

>> Operator with Strings

The >> operator skips any leading whitespace characters such as blanks and newlines

It then reads successive characters into the string, and stops at the first trailing whitespace character(which is not consumed, but remains waiting in the input stream)

24

String Input Using >>

string firstName;string lastName;cin >> firstName >> lastName;

Suppose input stream looks like this:

Joe Hernandez 23

What are the string values?

25

Results Using >>string firstName;string lastName;cin >> firstName >> lastName;

Result “Joe” “Hernandez”

firstName lastName

Interactive I/O

In an interactive program the user enters information while the program is executing

Before the user enters data, a prompt should be provided to explain what type of information should be entered

The amount of information needed in the prompt depends on the complexity of the data being entered, and the sophistication of the person entering the

data31

Prompting for Interactive I/O// DO: prompt the user to enter three integers and print out their average

32

Recommended