Docsity

Input Statements and Reading Data in C++: Examples and Explanations, Study notes of Engineering

Toggle sidebar
bg1
1
Electrical and Computer Engineering 1of 37
UAH CPE 112
Getting Data Into Programs
•An advantage of programs is that they can
be run many times using different sets of
data.
•A program must have some way of reading
the data (this is called input).
•An input stream is an endless sequence of
characters coming into your program.
•cin is associated with the standard input
device (keyboard). It has the extraction
operator >>.
•The extraction operator >> takes two
operands, cin and a variable.
Electrical and Computer Engineering 2of 37
UAH CPE 112
•InputStatement
cin >> Variable >> Variable …;
cin >> length >> width; is equal to
cin >> length;
cin >> width;
•Unlike the items specified in an output
statement, which can be constants,
variables, or complicated expressions,
the items specified in an input
statement can only be variable names.
Input Statement Syntax
and More
Electrical and Computer Engineering 3of 37
UAH CPE 112
Examples Using cin
int i, j, k; char ch; float x;
Statement Data Contents After Input
cin >> i; 32 i=32
Electrical and Computer Engineering 4of 37
UAH CPE 112
Examples Using cin
int i, j, k; char ch; float x;
Statement Data Contents After Input
cin >> i; 32 i=32
cin >> i 4 60 i=4
Electrical and Computer Engineering 5of 37
UAH CPE 112
Examples Using cin
int i, j, k; char ch; float x;
Statement Data Contents After Input
cin >> i; 32 i=32
cin >> i >> j; 4 60 i=4, j=60
Electrical and Computer Engineering 6of 37
UAH CPE 112
Examples Using cin
int i, j, k; char ch; float x;
Statement Data Contents After Input
cin >> i; 32 i=32
cin >> i >> j; 4 60 i=4, j=60
cin >> i 25 A 16.9 i=25
pf3
pf4
pf5

Partial preview of the text

Download Input Statements and Reading Data in C++: Examples and Explanations and more Study notes Engineering in PDF only on Docsity!

Electrical and Computer Engineering

1 of 37

Getting Data Into Programs

  • An advantage of programs is that they can

be run many times using different sets of

data.

  • A program must have some way of reading

the data (this is called input).

  • An input stream is an endless sequence of

characters coming into your program.

  • cin is associated with the standard input

device (keyboard). It has the extraction

operator >>.

  • The extraction operator >> takes two

operands, cin and a variable.

Electrical and Computer Engineering

2 of 37

  • InputStatement

cin >> Variable >> Variable …;

cin >> length >> width; is equal to

cin >> length; cin >> width;

  • Unlike the items specified in an output statement, which can be constants, variables, or complicated expressions, the items specified in an input statement can only be variable names.

Input Statement Syntax

and More

Electrical and Computer Engineering

3 of 37

UAH CPE 112

Examples Using cin

int i, j, k; char ch; float x; Statement Data Contents After Input cin >> i; 32 i=

Electrical and Computer Engineering

4 of 37

UAH CPE 112

Examples Using cin

int i, j, k; char ch; float x; Statement Data Contents After Input cin >> i; 32 i= cin >> i 4 60 i=

Electrical and Computer Engineering 5 of 37

UAH CPE 112

Examples Using cin

int i, j, k; char ch; float x; Statement Data Contents After Input cin >> i; 32 i= cin >> i >> j; 4 60 i=4, j=

Electrical and Computer Engineering 6 of 37

UAH CPE 112

Examples Using cin

int i, j, k; char ch; float x; Statement Data Contents After Input cin >> i; 32 i= cin >> i >> j; 4 60 i=4, j= cin >> i 25 A 16.9 i=

Electrical and Computer Engineering

7 of 37

Examples Using cin

int i, j, k; char ch; float x; Statement Data Contents After Input cin >> i; 32 i= cin >> i >> j; 4 60 i=4, j= cin >> i >> ch 25 A 16.9 i=25, ch=‘A’

Electrical and Computer Engineering

8 of 37

Examples Using cin

int i, j, k; char ch; float x; Statement Data Contents After Input cin >> i; 32 i= cin >> i >> j; 4 60 i=4, j= cin >> i >> ch >> x; 25 A 16.9 i=25,ch=‘A’,x=16.

Electrical and Computer Engineering

9 of 37

UAH CPE 112

Examples Using cin

int i, j, k; char ch; float x; Statement Data Contents After Input cin >> i; 32 i= cin >> i >> j; 4 60 i=4, j= cin >> i >> ch >> x; 25 A 16.9 i=25,ch=‘A’,x=16. cin >> i 25 A 16.9 i=

Electrical and Computer Engineering

10 of 37

UAH CPE 112

Examples Using cin

int i, j, k; char ch; float x; Statement Data Contents After Input cin >> i; 32 i= cin >> i >> j; 4 60 i=4, j= cin >> i >> ch >> x; 25 A 16.9 i=25,ch=‘A’,x=16. cin >> i >> ch 25 A 16.9 i=25,ch=‘A’

Electrical and Computer Engineering 11 of 37

UAH CPE 112

Examples Using cin

int i, j, k; char ch; float x; Statement Data Contents After Input cin >> i; 32 i= cin >> i >> j; 4 60 i=4, j= cin >> i >> ch >> x; 25 A 16.9 i=25,ch=‘A’,x=16. cin >> i >> ch >> x; 25 A 16.9 i=25,ch=‘A’,x=16.

Electrical and Computer Engineering 12 of 37

UAH CPE 112

Examples Using cin

int i, j, k; char ch; float x; Statement Data Contents After Input cin >> i; 32 i = 32 cin >> i >> j; 4 60 i = 4, j = 60 cin >> i >> ch >> x; 25 A 16.9 i=25,ch=‘A’,x=16. cin >> i >> ch >> x; 25 A 16.9 i=25,ch=‘A’,x=16. cin >> i 25A16.9 i=

Electrical and Computer Engineering

19 of 37

Examples Using cin

int i, j, k; char ch; float x; Statement Data Contents After Input cin >> i; 32 i = 32 cin >> i >> j; 4 60 i = 4, j = 60 cin >> i >> ch >> x; 25 A 16.9 i=25,ch=‘A’,x=16. cin >> i >> ch >> x; 25 A 16.9 i=25,ch=‘A’,x=16. cin >> i >> ch >> x; 25A16.9 i=25,ch=‘A’,x=16. cin >> i >> j >> x; 12 8 i = 12, j = 8 (Computer waits for a third number) cin >> i >> x; 46 32.4 15 i = 46, x = 32. (15 is held for later input)

Electrical and Computer Engineering

20 of 37

The Reading Marker

and the Newline Character

  • The reading marker indicates the next

character waiting to be read.

  • Each input line has an invisible end-of-line

character (the newline character) that tells

where one line ends and the next begins.

  • A newline character is inserted when you

hit an enter or Return or when a program

outputs an endl. It’s referred to as \n.

  • Examples:

ch = ‘\n’; or

cout << “Hello\n”;

(same as cout << “Hello” << endl;)

Electrical and Computer Engineering

21 of 37

UAH CPE 112

Exploring the Reading Marker

int i; char ch; float x;

Statements Contents After Input Marker Position

in the

Input Stream

25 A 16.9\n

Electrical and Computer Engineering

22 of 37

UAH CPE 112

Exploring the Reading Marker

int i; char ch; float x;

Statements Contents After Input Marker Position

in the

Input Stream

25 A 16.9\n

cin >> i; i = 25

Electrical and Computer Engineering 23 of 37

UAH CPE 112

Exploring the Reading Marker

int i; char ch; float x;

Statements Contents After Input Marker Position

in the

Input Stream

25 A 16.9\n

cin >> i; i = 25

cin >> ch; ch = ‘A’

Electrical and Computer Engineering 24 of 37

UAH CPE 112

Exploring the Reading Marker

int i; char ch; float x;

Statements Contents After Input Marker Position

in the

Input Stream

25 A 16.9\n

cin >> i; i = 25

cin >> ch; ch = ‘A’

cin >> x; x = 16.

Electrical and Computer Engineering

25 of 37

Another Reading

Marker Example

int i; char ch; float x;

Statements Contents After Marker Position in the

Input Input Stream

25\n

A\n

16.9\n

Electrical and Computer Engineering

26 of 37

Another Reading

Marker Example

int i; char ch; float x;

Statements Contents After Marker Position in the

Input Input Stream

25\n

A\n

16.9\n

cin >> i; i = 25

Electrical and Computer Engineering

27 of 37

UAH CPE 112

Another Reading

Marker Example

int i; char ch; float x;

Statements Contents After Marker Position in the

Input Input Stream

25\n

A\n

16.9\n

cin >> i; i = 25

cin >> ch; ch = ‘A’

Electrical and Computer Engineering

28 of 37

UAH CPE 112

Another Reading

Marker Example

int i; char ch; float x;

Statements Contents After Marker Position in the

Input Input Stream

25\n

A\n

16.9\n

cin >> i; i = 25

cin >> ch; ch = ‘A’

cin >> x; x = 16.

Electrical and Computer Engineering 29 of 37

UAH CPE 112

Reading Characters

withthe get Function

  • The >> operator skips any leading white-

space characters (such as blanks and new-

line characters) while looking for the next

data value in the input stream.

  • Consider cin >> ch1 >> ch2; and an input

stream of R 1. ch1 = ‘R’ ch2 = ‘1’ What if you

wanted the blank also? You can’t do it with

the extraction operator.

  • You can use the get function, which

doesn’t skip any characters.

cin.get(someChar);

  • The argument to the get function must be

a character variable.

Electrical and Computer Engineering 30 of 37

UAH CPE 112

The get Function versus

the Extraction Operator

char ch1, ch2, ch3; Statements Contents Marker Position in After Input the Input Stream A B\n CD\n cin >> ch1; ch1 = ‘A’

A B\n CD\n cin >> ch1; ch1 = ‘A’

Electrical and Computer Engineering

37 of 37

UAH CPE 112

Interactive I/O Example

Enter the part number: 4671 Enter the quantity of this part ordered: 10 Enter the unit price for this part:

Part 4671, quantity 10, at $ 27.25 each Totals $ 272.