Chapter 3 Exercise
Chapter 3 Exercise
Chapter 3 Exercise
EXERCISES
1. Mark the following statements as true or false.
a. The extraction operator >> skips all leading whitespace characters when
searching for the next data in the input stream.
b. In the statement cin >> x;, x must be a variable.
c. The statement cin >> x >> y; requires the input values for x and y to
appear on the same line.
172 | Chapter 3: Input/Output
d. The statement cin >> num; is equivalent to the statement num >> cin;.
e. You generate the newline character by pressing the Enter (return) key
on the keyboard.
f. The function ignore is used to skip certain input in a line.
2. Suppose num1 and num2 are int variables and symbol is a char variable.
Consider the following input:
47 18 * 28 $
What value (if any) is assigned to num1, num2, and symbol after each of the
following statements executes? (Use the same input for each statement.)
a. cin >> num1 >> symbol >> num2;
b. cin >> symbol >> num1 >> num2;
c. cin >> num1;
cin.get (symbol);
cin >> num2;
d. cin >> num1 >> num2;
cin.get (symbol);
e. cin.get (symbol);
cin >> num1 >> num2;
3. Suppose x and y are int variables and z is a double variable. Assume the
following input data:
37 86.56 32
What value (if any) is assigned to x, y, and z after each of the following
statements executes? (Use the same input for each statement.)
a. cin >> x >> y >> z;
b. cin >> x >> z >> y;
c. cin >> z >> x >> y;
4. Suppose x and y are int variables and symbol is a char variable. Assume
the following input data:
38 26 * 67 33
24 $ 55 # 34
# & 63 85
What value (if any) is assigned to x, y, and symbol after each of the
following statements executes? (Use the same input for each statement.)
a. cin >> x >> y;
cin.ignore(100, '\n');
cin >> symbol;
b. cin >> x;
cin.ignore(100, '*');
cin >> y;
cin.get(symbol);
Exercises | 173
c. cin >> y;
cin.ignore(100, '\n');
cin >> x >> symbol;
d. cin.get(symbol);
cin.ignore(100, '*');
cin >> x;
cin.ignore(100, '\n');
cin >> y;
3
e. cin.ignore(100, '\n');
cin >> x >> symbol;
cin.ignore(100, '\n');
cin.ignore(100, '&');
cin >> y;
int main()
{
int x, y;
string message;
double z;
x = 4;
y = 3;
z = 2.5;
z = pow(9.0, 2.5);
cout << z << endl;
return 0;
}
11. To use the functions peek and putback in a program, which header file(s)
must be included in the program?
12. Suppose that num is an int variable and discard is a char variable.
Assume the following input data:
#34
What value (if any) is assigned to num and discard after each of the
following statements executes? (Use the same input for each statement.)
a. cin.get (discard);
cin >> num;
b. discard = cin.peek();
cin >> num;
c. cin.get (discard);
cin.putback (discard);
cin >> discard;
cin >> num;
13. Suppose that name is a variable of type string. Write the input statement
to read and store the input Brenda Clinton in name. (Assume that the
input is from the standard input device.)
Exercises | 175
14. Write a C++ statement that uses the manipulator setfill to output a line
containing 35 stars, as in the following line:
***********************************
15. Suppose that age is an int variable and name is a string variable. What are
the values of age and name after the following input statements execute:
cin >> age;
getline(cin, name); 3
if the input is:
a. 23 Lance Grant
b. 23
Lance Grant
16. Suppose that age is an int variable, ch is a char variable, and name is a
string variable. What are the values of age and name after the following
input statements execute:
cin >> age;
cin.get(ch);
getline(cin, name);
int main()
{
int num1, num2;
ifstream infile;
outfile.open("output.dat");
infile >> num1 >> num2;
outfile << "Sum = " << num1 + num2 << endl;
return 0;
}
176 | Chapter 3: Input/Output
18. What may cause an input stream to enter the fail state? What happens when
an input stream enters the fail state?
19. Which header file needs to be included in a program that uses the data types
ifstream and ofstream?
20. Suppose that infile is an ifstream variable and employee.dat is a file
that contains employees’ information. Write the C++ statement that opens
this file using the variable infile.
21. A program reads data from a file called inputFile.dat and, after doing
some calculations, writes the results to a file called outFile.dat. Answer
the following questions:
a. After the program executes, what are the contents of the file
inputFile.dat?
b. After the program executes, what are the contents of the file outFile.dat if
this file was empty before the program executed?
c. After the program executes, what are the contents of the file outFile.dat if
this file contained 100 numbers before the program executed?
d. What would happen if the file outFile.dat did not exist before the
program executed?
22. Suppose that infile is an ifstream variable and it is associated with the
file that contains the following data: 27306 savings 7503.35. Write the
C++ statement(s) that reads and stores the first input in the int variable
acctNumber, the second input in the string variable accountType, and
the third input in the double variable balance.
23. Suppose that you have the following statements:
ofstream outfile;
double distance = 375;
double speed = 58;
double travelTime;
PROGRAMMING EXERCISES
1. Consider the following incomplete C++ program:
#include <iostream>
int main()
{
... 3
}
a. Write a statement that includes the header files fstream, string, and
iomanip in this program.
b. Write statements that declare inFile to be an ifstream variable and
outFile to be an ofstream variable.
c. The program will read data from the file inData.txt and write output to
the file outData.txt. Write statements to open both of these files, associate
inFile with inData.txt, and associate outFile with outData.txt.
d. Suppose that the file inData.txt contains the following data:
10.20 5.35
15.6
Randy Gill 31
18500 3.5
A
The numbers in the first line represent the length and width, respectively, of
a rectangle. The number in the second line represents the radius of a circle.
The third line contains the first name, last name, and the age of a person. The
first number in the fourth line is the savings account balance at the beginning
of the month, and the second number is the interest rate per year. (Assume
that p ¼ 3.1416.) The fifth line contains an uppercase letter between A and
Y (inclusive). Write statements so that after the program executes, the con-
tents of the file outData.txt are as shown below. If necessary, declare
additional variables. Your statements should be general enough so that if the
content of the input file changes and the program is run again (without
editing and recompiling), it outputs the appropriate results.
Rectangle:
Length = 10.20, width = 5.35, area = 54.57, parameter = 31.10
Circle:
Radius = 15.60, area = 764.54, circumference = 98.02
int main()
{}
double height;
return 0;
double radius;
cout << "Enter the radius of the base of the cylinder: ";
cin >> height;
cout << endl;
#include <iostream>
const double PI = 3.14159;
the user to enter the size of the fertilizer bag, in pounds, the cost of the bag,
and the area, in square feet, that can be covered by the bag. The program
should output the desired result. However, the program contains logic
errors. Find and correct the logic errors so that the program works properly.
//Logic errors.
#include <iostream>
#include <iomanip>
3
using namespace std;
int main()
{
double cost;
double area;
double bagSize;
cout << "Enter the area, in square feet, that can be "
<< "fertilized by one bag: ";
cin >> area;
cout << endl;
cout << "The cost of the fertilizer per pound is: $"
<< bagSize / cost << endl;
cout << "The cost of fertilizing per square foot is: $"
<< area / cost << endl;
return 0;
}
250 5750
100 28000
50 35750
25 18750
The first line indicates that the ticket price is $250 and that 5750 tickets were
sold at that price. Output the number of tickets sold and the total sale
amount. Format your output with two decimal places.
6. Redo Programming Exercise 21, in Chapter 2, so that each string can store
a line of text.
7. Three employees in a company are up for a special pay increase. You are
given a file, say Ch3_Ex7Data.txt, with the following data:
Each input line consists of an employee’s last name, first name, current salary,
and percent pay increase. For example, in the first input line, the last name of
the employee is Miller, the first name is Andrew, the current salary is
65789.87, and the pay increase is 5%. Write a program that reads data from
the specified file and stores the output in the file Ch3_Ex7Output.dat.
For each employee, the data must be output in the following form:
firstName lastName updatedSalary. Format the output of decimal
numbers to two decimal places.
8. Write a program that accepts as input the mass, in grams, and density, in
grams per cubic centimeters, and outputs the volume of the object using the
formula: volume ¼ mass / density. Format your output to two decimal places.
9. Interest on a credit card’s unpaid balance is calculated using the average daily
balance. Suppose that netBalance is the balance shown in the bill, payment is
the payment made, d1 is the number of days in the billing cycle, and d2
is the number of days payment is made before billing cycle. Then, the
average daily balance is:
If the interest rate per month is, say, 0.0152, then the interest on the
unpaid balance is:
Write a program that accepts as input netBalance, payment, d1, d2, and interest
rate per month. The program outputs the interest. Format your output to two
decimal places.
10. Linda is starting a new cosmetic and clothing business and would like to
make a net profit of approximately 10% after paying all the expenses, which
include merchandise cost, store rent, employees’ salary, and electricity cost
for the store. She would like to know how much the merchandise should 3
be marked up so that after paying all the expenses at the end of the year she
gets approximately 10% net profit on the merchandise cost. Note that after
marking up the price of an item she would like to put the item on 15% sale.
Write a program that prompts Linda to enter the total cost of the merchan-
dise, the salary of the employees (including her own salary), the yearly rent,
and the estimated electricity cost. The program then outputs how much the
merchandise should be marked up so that Linda gets the desired profit.