Concept explainers
Program plan:
- Declare a variable file of ifstream type to read the input from the file.
- Declare a variable letter of type char to store the letter read from the file.
- Declare 6 variables upperCounter, lowerCounter, blank, digit, punctuation, allElseCounter of type int to store the counter of each type.
- Declare a variable inFile of type String to store the name of the file read from the user.
- A do- while loop is used to read letters from the file and to increment the each type of counter.
- 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.
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:
Contents of the file Unclewill.txt:
Want to see more full solutions like this?
Chapter 7 Solutions
Programming and Problem Solving with C++: Comprehensive
- (use R language)Scatter plot(a). Run the R code example, and look at the help file for plot() function. Try different values for arguments:type, pch, lty, lwd, col(b). Use par(mfrow=c(3,2)) and output 6 plots with different argument settings.arrow_forward1. Draw flow charts for each of the following;a) A system that reads three numbers and prints the value of the largest number.b) A system reads an employee name (NAME), overtime hours worked (OVERTIME), hours absent(ABSENT) and determines the bonus payment (PAYMENT).arrow_forwardScenario You work for a small company that exports artisan chocolate. Although you measure your products in kilograms, you often get orders in both pounds and ounces. You have decided that rather than have to look up conversions all the time, you could use Python code to take inputs to make conversions between the different units of measurement. You will write three blocks of code. The first will convert kilograms to pounds and ounces. The second will convert pounds to kilograms and ounces. The third will convert ounces to kilograms and pounds. The conversions are as follows: 1 kilogram = 35.274 ounces 1 kilogram = 2.20462 pounds 1 pound = 0.453592 kilograms 1 pound = 16 ounces 1 ounce = 0.0283 kilograms 1 ounce = 0.0625 pounds For the purposes of this activity the template for a function has been provided. You have not yet covered functions in the course, but they are a way of reusing code. Like a Python script, a function can have zero or more parameters. In the code window you…arrow_forward
- make a screen capture showing the StegExpose resultsarrow_forwardWhich of the following is not one of the recommended criteria for strategic objectives? Multiple Choice a) realistic b) appropriate c) sustainable d) measurablearrow_forwardManagement innovations such as total quality, benchmarking, and business process reengineering always lead to sustainable competitive advantage because everyone else is doing them. a) True b) Falsearrow_forward
- Vision statements are more specific than strategic objectives. a) True b) Falsearrow_forwardThe three components of the __________ approach to corporate accounting include financial, environmental, and social performance measures. Multiple Choice a) stakeholder b) triple dimension c) triple bottom line d) triple efficiencyarrow_forwardCompetitors, as internal stakeholders, should be included in the stakeholder management consideration of a company and in its mission statement. a) True b) Falsearrow_forward
- At what level in the organization should the strategic management perspective be emphasized? Multiple Choice a) throughout the organization b) from the bottom up in an organization c) at the top of the organization d) at the middle of the organizationarrow_forwardA good manager can be flexible when it comes to sticking to the original plan; to get good results, the intended strategy has to become the realized strategy. a) True b) Falsearrow_forward________ tend to be quite enduring and seldom change. Multiple Choice a) Strategic objectives b) Vision statements c) Strategic plans d) Mission statementsarrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
- Systems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageProgramming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage Learning