How to find totals of different groups of characters in a file in C++

Viewed 75

Struggling with current file input/output. I know to use cctype to distinguish between the different groups but I have no idea how to get the total of each. Any help would be super appreciated Here's the assignment. Write a program to gather the number of times particular groups of characters appear in a file.

  1. Allow the user to specify which file to process.
  2. Count how often the following types of characters occur:
    • Alphabetic characters
    • Numeric characters
    • Control characters
    • Upper case characters
    • Lower case characters
    • Punctuation characters
    • Total number of characters in the file
  3. Have the program output the gathered results to a file name “stats.txt”.
  4. Display the information (left justified) in the order shown above.
  5. For each group of character types add a column that shows how often these characters appear.
  6. Add an additional column that shows what percentage that this groups comprises of the total number of characters in the file. Display this number with one decimal point. (Note: The percentage is not required on the line that display the total number of character processed.
  7. Please ensure all numerical columns are right justified, display appropriate precision and form a nice vertical table.

Here's my code so far:

enter code here
#include <iostream>
#include <cctype>
#include <fstream>
#include <iomanip>
#include <string>
//included libraries//

using namespace std;

//function definitions//

int main() {
int count;
int character;
string filename;
int alpha = 0, num = 0, con = 0, UC = 0, LC = 0, pun = 0, total = 0;

cout << "Enter file name and extension to process.\n";
cin >> filename;

ofstream fileout;
ifstream filein;

//input file//
filein.open(filename);
if (filein.fail()) {
cerr << "Failed to open the file: " << filename << endl;
}


//open file//
fileout.open("stats.txt");
if (fileout.fail()) {
cout << "Error, unable to open output file\n";
system("pause");
exit(1);
}
fileout.setf(ios::fixed);
fileout.setf(ios::showpoint);
fileout.precision(3);



filein.close();
fileout.close();
system("pause");
return 0;
}
1 Answers

I have no idea how to get the total of each

For this I suggest making a struct for a classification function containing:

  • The name, like Alphabetic.
  • A pointer to a classification function, like std::isalpha.
  • The total count.

The struct could look like this:

struct classifier {
    std::string_view heading;   // e.g. "Alphabetic"
    int (*class_func)(int);     // e.g. std::isalpha
    std::uintmax_t count = 0;   // start out with zero
};

You could then create an array of such structs:

std::array<classifier, 6> classifiers{{
    {"Alphabetic", std::isalpha},
    {"Numeric", std::isdigit},
    {"Control", std::iscntrl},
    {"Upper case", std::isupper},
    {"Lower case", std::islower},
    {"Punctuation", std::ispunct},
}};

Now, for each character you read from the file, you loop through the classifiers and check the character against the classification function and add to the count in the classifier if it was a match:

char ch = .. read from file ...;
++total_chars;

for(auto& [_, func, count] : classifiers) {
    count += func(static_cast<unsigned char>(ch)) != 0;
}

When the whole file has been read, the sum of each should be in each classifier:

for(auto& [heading, _, count] : classifiers) {
    std::cout << heading << " characters " << '\t' << count << '\n';
}
Related