Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Reading numbers is surprisingly difficult because a C++ input stream looks at th

ID: 3666161 • Letter: R

Question

Reading numbers is surprisingly difficult because a C++ input stream looks at the input one character at a time. First, white space is skipped. Then the stream con- sumes those input characters that can be a part of a number. Once the stream has recognized a number, it stops reading if it finds a character that cannot be a part of a number. However, if the first non-white space character is not a digit or a sign, or if the first character is a sign and the second one is not a digit, then the stream fails.

Consider a program reading an integer:

cout << "Enter the number of quarters: ";

int quarters;

cin >> quarters;

For each of the following user inputs, circle how many characters have been read and

whether the stream is in the failed state or not.

a. 15.9

b. 15 9

c. +159

d. -15A9

e. Fifteen

f. -Fifteen

g. + 15

h. 1.5E3

i. +1+5

Reading numbers is surprisingly difficult because a C++ input stream looks at the input one character at a time. First, white space is skipped. Then the stream con- sumes those input characters that can be a part of a number. Once the stream has recognized a number, it stops reading if it finds a character that cannot be a part of a number. However, if the first non-white space character is not a digit or a sign, or if the first character is a sign and the second one is not a digit, then the stream fails Consider a program reading an integer: cout "Enter the number of quarters:" int quarters; cin quarters;

Explanation / Answer


a. 15.9

quarters=15
After reading the integer value, the cin reads .(dot) operator.
Then the input stream cin fails to read next value.

------------------------------------------------------------------------------------------------------------------

b. 15 9

quarters=15
After reading the integer value, the cin reads white space character.
Then the input stream cin fails to read next value.

------------------------------------------------------------------------------------------------------------------

c. +159
quarters=159
The input stream cin stream reads integer value after sign. Then reads 159 then consumes integer value 159 and stops reading.
In this cin input stream not failed.

------------------------------------------------------------------------------------------------------------------

d. -15A9
quarters=-15
The input stream that cin stream reads. Then reads 15 then encounters a character 'A' .
Then cin input stream fails to read further.
Only -15 is consumed.

------------------------------------------------------------------------------------------------------------------

e. Fifteen
quarters=<garbage value>
The input to the cin stream if Fifteen . The cin>>quarters reads only integer values until a character is encountered.
The first character is F is countered then cin input stream fails to read . Then stores some garbage value if the quarters not initialized.

------------------------------------------------------------------------------------------------------------------

f.-Fifteen
quarters=<garbage value>
The input to the cin stream if -Fifteen .
If the first character is a sign and the second one is not a digit, then the stream fails.
The first character is read as sign and second character 'F' is not a digit then stream cin fails.

------------------------------------------------------------------------------------------------------------------

g.+15
quarters=15
The cin input stream reads further if the character is either - or + and continues to read input if the next letters are digits .

------------------------------------------------------------------------------------------------------------------

g. 1.5E3
quarters=1
The cin input stream reads digit 1 and stops reading input since the next character is not digit
fails to read next digits . The integer variable only takes integers values. 1.5 is double type value. cin fails to read after reading 1 into quarters.

------------------------------------------------------------------------------------------------------------------

i. +1+5
quarters=1
The cin input stream reads further if the character is either - or + and continues to read input if the next letter are digits and fails to read if the non-digit number is encounterd. after reading 1 , a non-digit letter + is encountered so
cin fails to read next input.