Part 1: Chapter 13-27
Part 2: Chapter 1-12
Solutions Manual Part 1: Chapter 13-27
Chapter 13
Programming Quiz 13.2: Question 1: (00000-11075)
Given a bool variable isReadable, write some statements that assign true to isReadable if the
file "topsecret" exists and can be read by the program and assigns false to isReadable
otherwise.
ifstream infile;
infile.open("topsecret");
isReadable = !infile.fail();
Programming Quiz 13.2: Question 2: (00000-11076)
Given an int variable x, write some statements that attempt to open a file named "table20" and
read a value into x; if that turns out not to be possible, your code should then read a value from console
input into x.
ifstream infile;
infile.open("table20");
if (infile.fail())
cin >> x;
else
infile >> x;
Programming Quiz 13.2: Question 3: (00000-11125)
Given the availability of an ofstream object named output and a string variable name tweet,
write the statements to open a file named "mytweet", display the prompt tweet: and then read an
entire line into tweet and then write it out to the file mytweet.
output.open("mytweet");
cout << "tweet:";
getline(cin, tweet);
output << tweet << endl;
output.close();
Programming Quiz 13.2: Question 4: (00000-11126)
Given the availability of a file named numbers, write the statements to read an int from standard input
and then read in that many values from numbers and display their total.
ifstream infile;
infile.open("numbers");
int total = 0;
int n, x;
cin >> n;
for (int i = 0; i < n; i++)
{
infile >> x;
total += x;
}
cout << total << endl;
,Programming Quiz 13.4: Question 1: (00000-11032)
A variable c of type char has been declared. Write the code to read in the next character from
standard input and store it in c, regardless of whether it is a whitespace character.
cin.get(c);
Programming Quiz 13.4: Question 2: (00000-11033)
Declare a string named line and write a statement that reads in the next line of console input into
this variable.
string line;
getline(cin, line);
Chapter 13: Programming Project 1: (345021-00054)
(Process scores in a text file)
Suppose that a text file Exercise13_3.txt contains an unspecified number of scores. Write a program
that reads the scores from the file and displays their total and average. Scores are separated by blanks.
For a hint on this program, please see https://liangcpp.pearsoncmg.com/cpprevel2e.html. If you get a
logic or runtime error, please refer to https://liangcpp.pearsoncmg.com/faq.html.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream input;
// Open a file
input.open("Exercise13_3.txt");
if (input.fail())
{
cout << "File does not exist" << endl;
cout << "Exit program" << endl;
return 0;
}
double number = 0;
double total = 0;
int count = 0;
while (!input.eof()) // Continue if not end of file
{
input >> number;
total += number;
count++;
}
input.close();
cout << "Total is " << total << endl;
, cout << "Average is " << total / count << endl;
return 0;
}
Chapter 13: Programming Project 2: (345021-00055)
(Create a text file)
Write a program to create a file named Exercise13_1.txt if it does not exist. If it does exist, append new
data to it. Write 100 integers 0 to 99 into the file using text I/O. Integers are separated by a space.
For a hint on this program, please see https://liangcpp.pearsoncmg.com/cpprevel2e.html. If you get a
logic or runtime error, please refer https://liangcpp.pearsoncmg.com/faq.html.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream output;
// Create a file
output.open("Exercise13_1.txt", ios::out | ios::app);
for (int i = 0; i < 100; i++)
{
output << i << " ";
}
output.close();
cout << "Done" << endl;
return 0;
}
Chapter 14
Programming Quiz 14.8: Question 1: (00000-11220)
Define and implement the following Window class:
- int data members, width and height.
- a constructor that accepts two int parameters (width followed by height) and uses them to
initialize the data members.
- a friend function, areSameSize, that accepts two Window objects and returns a bool indicating if
they are of the same size. Two Window objects are of the same size if their widths and heights match.
class Window
{
public:
Window(int width, int height): width(width), height(height) {}
friend bool areSameSize(const Window& w1, const Window& w2)
{
return w1.width == w2.width && w1.height == w2.height;
}
, private:
int width, height;
};
Programming Quiz 14.9: Question 1: (00000-11226)
Assume the existence of a Window class with int data members width and height.
Overload the << operator for the Window class-- i.e.,
write a nonmember ostream-returning function that accepts a reference to an ostream object and a
constant reference to a Window object and sends the following to the ostream:
'a (width x height) window' (without the quotes and with width and height replaced by the
actual width and height of the window.
Thus for example, if the window had width=80 and height=20, << would send 'a (80 x
20) window' to the ostream.
Don't forget to have the function return the proper value as well.
Note: The Window class is already in place. Declare and implement a friend << operator function
inside the Window class.
friend ostream& operator<<(ostream& os, const Window& window)
{
os << "a (" << window.width << " x " << window.height << ") window";
return os;
}
Programming Quiz 14.9: Question 2: (00000-11227)
Assume the existence of a Window class with int data members width and height.
Overload the >> operator for the Window class-- i.e.,
write a nonmember istream-returning function that accepts a reference to an istream object and a
reference to a Window object and reads the next two values from the istream into
the width and height members respectively.
Note: The Window class is already in place. Declare and implement a friend << operator function
inside the Window class.
friend istream& operator>>(istream& is, Window& window)
{
is >> window.width >> window.height;
return is;
}
Chapter 14: Programming Project 1: (345021-00168)
(Math: The Complex class)
The description of this project is given in Programming Exercise 14.7 in the Chapter 14 Programming
Exercise from the Book section.