Lesson 7-2: Multiway Branching
160 | Chapter 7
Name __________________________________________ Date _______________________
Section _________________________________________
Exercise 1: As Prelab Exercise 4 demonstrated, program Switches is not very robust. Add the
code necessary to allow the program to work properly with both lowercase and uppercase versions
of the input letters. Run your program with the same data, but type in the letters as lowercase.
Exercise 2: Program Switches is still not very robust. Add a default case that prints an error
message and asks for the letter to be reentered. Test your program with the same data set, but add
several letters that are not correct.
Exercise 3: Program CountMarks is the shell of a program that counts all the punctuation marks
in a file.
// Program CountMarks counts punctuation marks in a file.
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ifstream inData;
char symbol;
int periodCt = 0;
int commaCt = 0;
int questionCt = 0;
int colonCt = 0;
int semicolonCt = 0;
inData.open("switch.dat");
/* FILL IN */
return 0;
}
Fill in the missing code and run your program. Show the counts in the following table.
Periods: ________ Commas: ________ Semicolons: ________
Question marks: ________ Colons: ________
78828_CH07_DaleLab 5/21/09 10:48 AM Page 160
Exercise 4: Add the code necessary for program CountMarks to count blanks as well.
How many blanks are there in file switch.dat? If you did not get a count of 12, go
back and check your program.
Additional Control Structures | 161
78828_CH07_DaleLab 5/21/09 10:48 AM Page 161

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.