What & How & Why

Chapter.8

Answers for chapter 8


ex.8.1
Exercise 8.1: Write a function that takes and returns an istream&. The function should read the stream until it hits end-of-file. The function should print what it reads to the standard output. Reset the stream so that it is valid before returning the stream.

std::istream& 
reader(std::istream& is) {
	std::string word;
	while(is >> word) {
			std::cout << word << std::endl;
	}
	is.clear();
	return is;
}

ex.8.2
Exercise 8.2: Test your function by calling it, passing cin as an argument.

CODE: Q.1

ex.8.3
Exercise 8.3: What causes the following while to terminate?

while (cin >> i) /* ... */
Execpt for goodbit, all other flags will cause while statement to terminate.

ex.8.4
Exercise 8.4: Write a function to open a file for input and read its contents into a vector of strings, storing each line as a separate element in the vector.

CODE: Q.1

ex.8.5
Exercise 8.5: Rewrite the previous program to store each word in a separate element.

CODE: Q.1

ex.8.6
Exercise 8.6: Rewrite the bookstore program from § 7.1.1 (p. 256) to read its transactions from a file. Pass the name of the file as an argument to main (§ 6.2.5, p. 218).

CODE: headar source

ex.8.7
Exercise 8.7: Revise the bookstore program from the previous section to write its output to a file. Pass the name of that file as a second argument to main.

CODE: Q.1

ex.8.8
Exercise 8.8: Revise the program from the previous exercise to append its output to its given file. Run the program on the same output file at least twice to ensure that the data are preserved.

CODE: Q.1

ex.8.9
Exercise 8.8: Revise the program from the previous exercise to append its output to its given file. Run the program on the same output file at least twice to ensure that the data are preserved.

CODE: Q.1

ex.8.10
Exercise 8.8: Revise the program from the previous exercise to append its output to its given file. Run the program on the same output file at least twice to ensure that the data are preserved.

CODE: Q.1

ex.8.11
The program in this section defined its istringstream object inside the outer while loop. What changes would you need to make if record were defined outside that loop? Rewrite the program, moving the definition of record outside the while, and see whether you thought of all the changes that are needed.

The program will print either nothing or whatever string “line” has by default.

The istringstream object defined in a loop is a temporary object. It is created, destroyed and bound to the newest line from the input each loop. That allows us to bind an isstringstream object to every line of the outer loop, then print every element in the line. If we choose to move the object definition out of the outer loop, it will be bound to the default, empty string “line” and will print out nothing(or default value in that string). The contents added to the “line” afterward through the getline function will not affect the result.

/* test result */
//input
aaa 111
bbb 222
ccc 333
ddd 444
//output, the string line has default value "Hello word."
Hello word.
CODE: Q.1

ex.8.12
Exercise 8.12: Why didn’t we use in-class initializers in PersonInfo?

The Personalinfo class has two members, a string type member and a vector<string> type member. Both members can provide a safe default value. Hence, there is no need to specify initializers for them.

ex.8.13
Exercise 8.13: Rewrite the phone number program from this section to read from a named file rather than from cin.

CODE: Q.1

ex.8.14
Exercise 8.14: Why did we declare entry and nums as const auto &?
  • Adding const: Modifications are no needed for entry and nums.
  • Adding reference: Passing a string with a reference is more efficient.