Lesson 7-3: Additional Control Structures
162 | Chapter 7
Name __________________________________________ Date _______________________
Section _________________________________________
Use program Looping for Exercises 1 through 5. This program reads and sums exactly ten
integers and then reads and sums integers until a negative value is read.
// Program Looping uses a count-controlled loop to read and
// sum 10 integer values and an event-controlled loop to
// read and sum values until a negative value is found.
// The data is on file looping.dat.
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ifstream inData;
int value;
int counter;
int sum;
inData.open("looping.dat");
counter = 1;
sum = 0;
while (counter <= 10)
{ // 10 values read and summed
inData >> value;
sum = sum + value;
counter++;
}
cout << "The first sum is " << sum << endl;
inData >> value;
sum = 0;
while (value >= 0)
{ // Values are read and summed until a negative is read
sum = sum + value;
inData >> value;
}
cout << "The second sum is " << sum << endl;
return 0;
}
78828_CH07_DaleLab 5/21/09 10:48 AM Page 162
Exercise 1: Compile and run program Looping. Fill in the following table.
First sum: __________
Second sum: __________
Exercise 2: Program Looping contains two loops implemented with while statements.
Rewrite program Looping, replacing the while statements with do-while statements.
Fill in the following table.
First sum: __________
Second sum: __________
Exercise 3: Can program Looping be rewritten using a for statement for each loop?
Explain.
Exercise 4: Rewrite program Looping using a for statement to implement the count-
controlled loop. Fill in the following table.
First sum: __________
Second sum: __________
Exercise 5: Rerun your program using data file looping.d2. Describe what happens.
If an error condition was generated, correct your program and rerun the program. Fill
in the following table.
First sum: __________
Second sum: __________
Additional Control Structures | 163
78828_CH07_DaleLab 5/21/09 10:48 AM Page 163
Exercise 6: Here is yet another version of program Convert, which converts
temperatures from Fahrenheit to Celsius or from Celsius to Fahrenheit.
// Program Convert converts temperatures between Fahrenheit and
// Celsius, depending on whether the user enters an F or a C.
#include <iostream>
using namespace std;
int main ()
{
char letter; // Place to store input letter
int tempIn; // Temperature to be converted
int tempOut; // Converted temperature
cout << "Input Menu" << endl << endl;
cout << "F: Convert from Fahrenheit to Celsius" << endl;
cout << "C: Convert from Celsius to Fahrenheit" << endl;
cout << "Q: Quit " << endl;
cout << "Type a C, F, or Q; then press return." << endl;
cin >> letter;
while (letter != 'Q')
{
cout << "Type an integer number and press return." << endl;
cin >> tempIn;
if (letter == 'F')
{
tempOut = 5 * (tempIn - 32) / 9;
cout << tempIn << " in Fahrenheit is " << tempOut
<< " in Celsius." << endl;
}
else
{
tempOut = (9 * tempIn / 5) + 32;
cout << tempIn <<F " in Celsius is " << tempOut
<< " in Fahrenheit." << endl;
}
cout << "Type a C, F, R, or Q; then press return." << endl;
cin >> letter;
}
return 0;
}
Rewrite program Convert replacing the if statements with switch statements. Make
your program as robust as possible. (Recall that “robust” is a descriptive term for a
program that can recover from erroneous inputs and keep running.) Run your program
and fill in the table on the next page.
164 | Chapter 7
78828_CH07_DaleLab 5/21/09 10:48 AM Page 164
Code Temperature Result
C 0 ___________
F 32 ___________
F 134 ___________
C 56 ___________
Q Run terminated
Exercise 7: There is no error checking in program Convert. Rewrite the solution to
Exercise 6 so that the program accepts both uppercase and lowercase letters. If a letter
other than F, C, or Q is entered, print an error message and ask for the letter to be
reentered.
Additional Control Structures | 165
78828_CH07_DaleLab 5/21/09 10:48 AM Page 165

Get A Laboratory Course in C++, 5th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.