Programming and Problem Solving With C++
Programming and Problem Solving With C++
6th Edition
ISBN: 9781449694265
Author: Nell Dale
Publisher: Jones & Bartlett Learning
bartleby

Concept explainers

Question
Book Icon
Chapter 7, Problem 1PSCS
Program Plan Intro

Program plan:

  1. Declare a variable file of ifstream type to read the input from the file.
  2. Declare a variable letter of type char to store the letter read from the file.
  3. Declare 6 variables upperCounter, lowerCounter, blank, digit, punctuation, allElseCounter of type int to store the counter of each type.
  4. Declare a variable inFile of type String to store the name of the file read from the user.
  5. A do- while loop is used to read letters from the file and to increment the each type of counter.
  6. A switch is used to check for each type of letters and to increment the counter inside do-while loop.

Program description:

The main purpose of the program is to summarize all the contents of the file in a table with details of each type of character scanned.

Expert Solution & Answer
Check Mark

Explanation of Solution

Program:

//inclusion of header files
#include <fstream> 
#include <iostream> 
#include <iomanip>
#include <cctype> 
//using namespace
using namespace std;
//main function
int main() 
{
ifstream file; //fstream input
char letter; 
// Declaration and Initialization of variables
int upperCounter = 0; // counter for uppercase letters
int lowerCounter = 0; // counter for lowercase letters
int blank = 0; // counter for blanks
int digit = 0; //counter for digits
int punctuation = 0; //counter for punctuation
int allElseCounter = 0; //counter for Remaining counters
//Declare variable to store filename
string inFile;
cout<<"Enter the filename to be processed" << endl;
cin>>inFile;
//open the file
file.open(inFile.c_str()); 
//if file doesn’t get opened
if (!file) 
{
    cout<< "Filename doesn’t exist." << endl;
    return 1; 
}
//else read the characters 
file.get(letter); // Input one letter 
do 
// process each letter
{
if (isupper(letter)) 
upperCounter++; 
else if (islower(letter)) 
lowerCounter++; 
else if (isdigit(letter)) 
digit++;
else 
switch (letter) 
{ 
case ' ' : blank++;
            break;
case '.' :
case '?' :
case '!' : punctuation++; 
            break; 
default : allElseCounter++; 
            break; 
} 
file.get(letter); 
} while (file);
// Calculate total 
float total = upperCounter + lowerCounter + blank + digit + punctuation + allElseCounter;
cout<<"Summary of letters: "<<  inFile << endl;
// Letters of each type
cout<<fixed<<setprecision(3)<<"Percentage of uppercase letters:"<<upperCounter / total* 100<<endl; 
cout<<fixed<<setprecision(3)<< "Percentage of lowercase letters:" << lowerCounter / total * 100 << endl; 
cout<<fixed<<setprecision(3)<< "Percentage of blanks: "<< blank / total * 100 << endl;
cout<<fixed<<setprecision(3)<< "Percentage of digits: "<< digit / total * 100 << endl; 
cout<<fixed<<setprecision(3)<< "Percentage of end-of-sentence: "<< "punctuation "<< punctuation / total * 100 << endl;
return 0;
}

Explanation:

In this program, first of all, the user is asked to enter the filename which needs to be processed. After entering the filename, all the characters of it are scanned to get a summary table of each type of letters. Each letter is scanned for uppercase letters, lowercase letters, for digits, blanks, and for any puctuations. Each type counter get incremented if the letter scanned is of that type. For example, if the letter scanned is of uppercase then the counter for uppercase letters will get incremented and so on. In the end, complete summary of all types of letters is printed on the screen with the filename which is scanned as shown in the output.

Output:

Programming and Problem Solving With C++, Chapter 7, Problem 1PSCS , additional homework tip  1

Contents of the file Unclewill.txt:

Programming and Problem Solving With C++, Chapter 7, Problem 1PSCS , additional homework tip  2

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Three numbersa. Write a program that reads three numbers and prints “all the same” if they are allthe same, “all different” if they are all different, and “neither” otherwise.b. Write a program that reads three numbers and prints “increasing” if they are inincreasing order, “decreasing” if they are in decreasing order, and “neither”otherwise. Here, “increasing” means “strictly increasing”, with each value largerthan its predecessor. The sequence 3 4 4 would not be considered increasing.c. Repeat part b. but before reading the numbers, ask the user whetherincreasing/decreasing should be “strict” or “lenient”. In lenient mode, thesequence 3 4 4 is increasing and the sequence 4 4 4 is both increasing anddecreasing
please do not use java    Your program must use the following functions: printHeader(…): prints the two lines header of the table shown in the sampleoutput getData(…): receives a string representing line of text read from file. Itvalidates and returns name, job code and days worked, and a Boolean value ofTrue of the line contains valid data or False if the line contains faulty data. computeSalary(…):receives the job code and days worked and computes and returnsthe employee salary main():Opens input file for reading, reads data from file, calls variousfunctions and prints results
Consider records of employees each consists of employee's first name (string), number of hours worked (int)and hourly rate in Bahraini Dinar BD. (double).Write a program that reads from the user multiple records for multiple employees and displays the following onscreen:(a) the salary for each employee (Salary = Hours Worked x Hourly rate.)(b) the number of employees(c) the highest salary with employee name.Reading records of employees should stop when the user enter `stop' for the employee name. Sample I/O of theprogram is shown in the example below.

Chapter 7 Solutions

Programming and Problem Solving With C++

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education