An Introduction to Programming with C++: Eighth Edition
Chapter 14 Answers
Review Questions
1. a field
2. c record
3. b fstream
4. c ofstream
5. b ofstream outPayroll;
Exercises Pencil and Paper
1. The answer to this TRY THIS Exercise is located at the end of Chapter 14 in the book.
2. The answer to this TRY THIS Exercise is located at the end of Chapter 14 in the book.
3. outJan.open(“janSales.txt”, ios::app);
4. inInventory.open(“inventory.txt”); or inInventory.open(“inventory.txt,
ios::in);
5. outSales.open(“firstQtr.txt”, ios::app);
6. outFeb.open(“febSales.txt”); or outFeb.open(“febSales.txt”, ios::out);
7. if (outSales.is_open()) or if (outSales.is_open() == true)
Exercises Computer
16. The answer to this TRY THIS Exercise is located at the end of Chapter 14 in the book. The code and text
files are contained in the TryThis16.cpp and TryThis16.txt files, respectively. The files are contained in
either the Cpp8\Chap14\TryThis16 Project-IM folder (Microsoft) or the Cpp8\Chap14 folder (non
Microsoft).
17. The answer to this TRY THIS Exercise is located at the end of Chapter 14 in the book. The code and text
files are contained in the TryThis17.cpp and TryThis17.txt files, respectively. The files are contained in
either the Cpp8\Chap14\TryThis17 Project-IM folder (Microsoft) or the Cpp8\Chap14 folder (non
Cpp8\Chap14\Introductory20 Project-IM folder (Microsoft) or the Cpp8\Chap14 folder (non-Microsoft).
21. See the Introductory 21.cpp and Introductory21.txt files, which are contained in either the
Cpp8\Chap14\Introductory 21 Project-IM folder (Microsoft) or the Cpp8\Chap14 folder (non-Microsoft).
22. See the Intermediate22.cpp and Intermediate22.txt files, which are contained in either the
Cpp8\Chap14\Intermediate22 Project-IM folder (Microsoft) or the Cpp8\Chap14 folder (non-Microsoft).
23. See the Intermediate23.cpp and Intermediate23.txt files, which are contained in either the
Cpp8\Chap14\Intermediate23 Project-IM folder (Microsoft) or the Cpp8\Chap14 folder (non-Microsoft).
24. See the Intermediate24.cpp and Intermediate24.txt files, which are contained in either the
Cpp8\Chap14\Intermediate24 Project-IM folder (Microsoft) or the Cpp8\Chap14 folder (non-Microsoft).
29. See the Advanced29.cpp, Advanced29.txt, and EvenAdvanced29.txt files, which are contained in either the
Cpp8\Chap14\Advanced29 Project-IM folder (Microsoft) or the Cpp8\Chap14 folder (non-Microsoft).
30. See the SwatTheBugs30.cpp and SwatTheBugs30.txt files, which are contained in either the
Cpp8\Chap14\SwatTheBugs30 Project-IM folder (Microsoft) or the Cpp8\Chap14 folder (non-Microsoft).
The modifications made to the program are shaded below.
//SwatTheBugs30.cpp – displays the contents of a sequential access file
//Created/revised by <your name> on <current date>
{
string name = “”;
int num1 = 0;
int num2 = 0;
ifstream inFile;
inFile.open(“SwatTheBugs30.txt”);
if (inFile.is_open())
{
while (!inFile.eof())
{
//display the record
cout << name << “, ” << num1 << “, ” << num2 << endl;
//read another record
getline(inFile, name, ‘#’);