Title: Programming in C
1Programming in C
- Dale/Weems/Headington
- Chapter 4
- Program Input and the Software Design Process
2Extraction Operator ( gtgt )
- Variable cin is predefined to denote an input
stream from the standard input device ( the
keyboard ). - The extraction operator gtgt 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 gtgt attempts to extract the next item
from the input stream and store its value in the
right operand variable.
3Input Statements
- SYNTAX
- These examples yield the same result.
- cin gtgt length
- cin gtgt width
- cin gtgt length gtgt width
cin gtgt Variable gtgt Variable . . .
4Whitespace 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.
5Extraction Operator gtgt
- 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)
6At keyboard you type AspaceBspaceCEnter
char first char middle char last
cin gtgt first cin gtgt middle
cin gtgt last NOTE A file reading marker
is left pointing to the newline character
after the C in the input stream.
first
middle
last
A
B
C
first
middle
last
7At keyboard you typespace25spaceJspace2En
ter
int age char initial float
bill cin gtgt age cin gtgt
initial cin gtgt bill NOTE A file
reading marker is left pointing to the
newline character after the 2 in the input
stream.
25
J
2.0
8Keyboard and Screen I/O
9Diskette files for I/O
10To Use Disk I/O, you must
- use include ltfstream.hgt
- choose valid variable identifiers for your files
and declare them - open the files and associate them with disk names
- use your variable identifiers with gtgt and ltlt
- close the files
11Statements for using Disk I/O
- include ltfstream.hgt
- ifstream myInfile // declarations
- ofstream myOutfile
-
- myInfile.open(A\\myIn.dat) // open files
- myOutfile.open(A\\myOut.dat)
- myInfile.close( ) // close files
- myOutfile.close( )
12What does opening a file do?
- associates the C identifier for your file with
the physical (disk) name for the file - if the input file does not exist on disk, open is
not successful - if the output file does not exist on disk, a new
file with that name is created - if the output file already exists, it is erased
- places a file reading marker at the very
beginning of the file, pointing to the first
character in it
13Another example using gtgt
STATEMENTS CONTENTS MARKER
POSITION int i 25 A\n char
ch 16.9\n float x cin gtgt i
25 A\n 16.9\n cin gtgt ch
25 A\n 16.9\n cin gtgt x
25 A\n 16.9\n
25
16.9
14Another way to read char data
The get( ) function can be used to read a single
character. It obtains the very next character
from the input stream without skipping any
leading whitespace characters. skipping any
leading whitespace
15At keyboard you type AspaceBspaceCEnter
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
A
B
first
middle
last
16Use 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.
17An example using cin.ignore( )
STATEMENTS CONTENTS MARKER
POSITION int a 957 34 1235\n
int b 128 96\n int c cin
gtgt a gtgt b 957 34 1235\n 128
96\n cin.ignore(100, \n) 957 34
1235\n 128 96\n cin gtgt c
957 34 1235\n 128 96\n
18Another example using cin.ignore( )
STATEMENTS CONTENTS
MARKER POSITION int i
A 22 B 16 C 19\n char ch
cin gtgt ch A 22 B 16 C
19\n cin.ignore(100, B)
A 22 B 16 C 19\n cin gtgt i
A 22 B 16 C 19\n
i
ch
i
ch
i
ch
i
ch
19Interactive 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. - After the user enters data, the value of the data
should be printed out for verification. This is
called echo printing. - That way, the user will have the opportunity to
check for erroneous data.
20Prompting for Interactive I/O
- cout ltlt Enter part number ltlt endl
// prompt - cin gtgt partNumber
- cout ltlt Enter quantity ordered ltlt endl
- cin gtgt quantity
- cout ltlt Enter unit price ltlt endl
- cin gtgt unitPrice
- totalPrice quantity unitPrice
// calculate - cout ltlt Part ltlt partNumber ltlt endl
// echo - cout ltlt Quantity ltlt quantity ltlt endl
- cout ltlt Unit Cost ltlt setprecision(2)
- ltlt unitPrice ltlt endl
- cout ltlt Total Cost ltlt totalPrice ltlt
endl
21File I/O Mileage Program
/ This program computes miles per gallon given
four amounts for gallons used, and starting
and ending mileage. Input (file)
The gallon amounts for four fillups.
The starting mileage. The ending
mileage. Output (file) The
calculated miles per gallon. - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - -/ include ltiostream.hgt include
ltfstream.hgt // for file I/O
22C Code Continued
int main(void) float amt1 //
Number of gallons for fillup 1 float
amt2 // Number of gallons for fillup 2
float amt3 // Number of gallons for
fillup 3 float amt4 // Number
of gallons for fillup 4 float
startMiles // Starting mileage
float endMiles // Ending mileage
float mpg // Computed miles
per gallon ifstream inMPG // File holds
gallons and mileage ofstream outMPG //
File holes miles per gallon result
inMPG.open ( inmpg.dat ) // open files
outMPG.open ( outmpg.dat )
23End of Program
// get data from file inMPG gtgt amt1
gtgt amt2 gtgt amt3 gtgt amt4 gtgt
startMiles gtgt endMile // calculate
mpg (endMiles - startMiles) / (amt1 amt2
amt3 amt4) // write output to file
outMPG ltlt For the gallon amounts ltlt endl
outMPG ltlt amt1 ltlt ltlt amt2 ltlt
ltlt amt3 ltlt ltlt amt4 ltlt endl
outMPG ltlt and starting mileage ltlt
startMiles ltlt endl outMPG ltlt and
ending mileage ltlt endMiles ltlt endl
outMPG ltlt the mileage per gallon is ltlt mpg
ltlt endl return 0
24 Stream Fail State
- When a stream enters the fail state, further I/O
operations using that stream have no effect at
all. But the computer does not automatically
halt the program or give any error message. - Possible reasons for entering fail state include
- invalid input data (often the wrong type),
- opening an input file that doesnt exist,
- opening an output file on a diskette that is
already full or is write-protected.
25Functional Decomposition
A technique for developing a program in which the
problem is divided into more easily handled
subproblems, the solutions of which create a
solution to the overall problem. In functional
decomposition, we work from the abstract (a list
of the major steps in our solution) to the
particular (algorithmic steps that can be
translated directly into code in C or another
language).
26Functional Decomposition
FOCUS is on actions and algorithms. BEGINS by
breaking the solution into a series of major
steps. This process continues until each
subproblem cannot be divided further or has an
obvious solution. UNITS are modules
representing algorithms. A module is a
collection of concrete and abstract steps that
solves a subproblem. A module structure chart
(hierarchical solution tree) is often created.
DATA plays a secondary role in support of
actions to be performed.
27Module Structure Chart
Main
Get Data
Prepare File for Reading
Print Data
Print Heading
28Object-Oriented Design
A technique for developing a program in which the
solution is expressed in terms of objects --
self- contained entities composed of data and
operations on that data.
29More about OOD
- Languages supporting OOD include C, Java,
Smalltalk, Eiffel, CLOS, and Object-Pascal. - A class is a programmer-defined data type and
objects are variables of that type. - In C, cin is an object of a data type (class)
named istream, and cout is an object of a class
ostream. Header files iostream.h and fstream.h
contain definitions of stream classes. - A class generally contains private data and
public operations (called member functions).
30Object-Oriented Design (OOD)
FOCUS is on entities called objects and
operations on those objects, all bundled
together. BEGINS by identifying the major
objects in the problem, and choosing appropriate
operations on those objects. UNITS are
objects. Programs are collections of objects
that communicate with each other. DATA plays a
leading role. Algorithms are used to implement
operations on the objects and to enable
interaction of objects with each other.
31Why use OOD with large software projects?
- Objects within a program often model real-life
objects in the problem to be solved. - Many libraries of pre-written classes and objects
are available as-is for re-use in various
programs. - The OOD concept of inheritance allows the
customization of an existing class to meet
particular needs without having to inspect and
modify the source code for that class. This can
reduce the time and effort needed to design,
implement, and maintain large systems.