PROGRAM+PROBLEM SOLVING W/C++,BRF (SPEC
PROGRAM+PROBLEM SOLVING W/C++,BRF (SPEC
6th Edition
ISBN: 9781284083781
Author: Dale
Publisher: JONES+BART
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:

PROGRAM+PROBLEM SOLVING W/C++,BRF (SPEC, Chapter 7, Problem 1PSCS , additional homework tip  1

Contents of the file Unclewill.txt:

PROGRAM+PROBLEM SOLVING W/C++,BRF (SPEC, 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
You are given a filename. Read the names in the file and output only the initials. For example, if the filename is "data.txt", and the file content in   "data.txt" is   Dumond, Kristin   Larson, Lois   the output should be:   KD   LL
(5) Write a function called copy_even_lines() that takes two strings (filenames) as input parameters and copies every other line from the first file into the second. That is, counting from 0, it copies the 0th line, the 2nd line, the 4th line, etc. from the first file to the second file. You may assume that both files exist. (6) Write a python program that asks the user for two filenames and calls your function copy_even_lines() from (5) using those two filenames. If one or more files do not exist, your program must handle the exception by repeating the prompts until valid filenames are entered.
Question B. Write a C++ program that reads the marks of a group of students from a text file "grades.txt" and computes the average of all grades. The program must check the file for any opening problems, and then proceeds to do the other tasks. The input file can have 1 or more students. The text file and sample-output are shown as below: O grades - Notepad File Edit Format View Help 60.0 78.6 78.0 98.6 77.7 55.0 73.0 80.7 80.0 85.0 Windows (CF Ln 13, Col 1 100% CAUsers\Imran\Desktop\Lab111\Debug\Lab111.exe Average of 10 students is 77 Press any key to continue... Activa Coto s

Chapter 7 Solutions

PROGRAM+PROBLEM SOLVING W/C++,BRF (SPEC

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
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage