19
1 Program Input and the Software Design Process Dale/Weems

Chapter 4 Program Input and the Software Design Process

  • Upload
    elwyn

  • View
    35

  • Download
    2

Embed Size (px)

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

Page 1: Chapter 4 Program Input and the Software Design Process

1

Chapter 4

Program Input and the

Software Design Process

Dale/Weems

Page 2: Chapter 4 Program Input and the Software Design Process

2

Input Statements to Read Values into a Program using >>

Prompting for Interactive Input/Output

Page 3: Chapter 4 Program Input and the Software Design Process

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

output stream

Keyboard Screenexecutingprogram

ostreamistream

3

Page 4: Chapter 4 Program Input and the Software Design Process

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.

Page 5: Chapter 4 Program Input and the Software Design Process

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

Page 6: Chapter 4 Program Input and the Software Design Process

<iostream> Header File

Access to a library that defines 2 objects

An istream object named cin (keyboard)

An ostream object named cout (screen)

6

Page 7: Chapter 4 Program Input and the Software Design Process

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;

Page 8: Chapter 4 Program Input and the Software Design Process

>> 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

Page 9: Chapter 4 Program Input and the Software Design Process

SYNTAX

These examples yield the same result.

cin >> length;

cin >> width;

cin >> length >> width;

Input Statements

cin >> Variable >> Variable . . .;

10

Page 10: Chapter 4 Program Input and the Software Design Process

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

Page 11: Chapter 4 Program Input and the Software Design Process

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

Page 12: Chapter 4 Program Input and the Software Design Process

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

Page 13: Chapter 4 Program Input and the Software Design Process

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’

Page 14: Chapter 4 Program Input and the Software Design Process

22

Example

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

However . . .

String Input in C++

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

Page 15: Chapter 4 Program Input and the Software Design Process

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)

Page 16: Chapter 4 Program Input and the Software Design Process

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?

Page 17: Chapter 4 Program Input and the Software Design Process

25

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

Result “Joe” “Hernandez”

firstName lastName

Page 18: Chapter 4 Program Input and the Software Design Process

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

Page 19: Chapter 4 Program Input and the Software Design Process

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

32