I need help with I/O files.
Nov 6, 2016 at 5:40pm UTC
So, we need to make a program that lets your enter a file that and counts the characters such as lowercase, uppercase, punctuations, etc then it outputs it to another file. We also need to output the percentage of the characters the file has.
I can't seem to make it work. It just gives me the "Error has been detected when opening the file."
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#include <iostream>
#include <cctype>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
ifstream inFile;
ofstream outFile;
int countUpper = 0, countLower = 0, countControl = 0, countPunct = 0, countAlpha = 0, countNum = 0;
char character;
double percAlpha, percNum, percUpper, percLower, percControl, percPunct;
double total = countUpper + countLower + countControl + countPunct + countAlpha + countNum;
string filename;
std::cout << "Please enter the file name: " ;
cin >> filename;
inFile.open(filename);
if (inFile.fail()) {
std::cout << "Error has been detected when opening a file." << endl;
system("pause" );
return 1;
}
outFile.open("stats.txt" );
if (outFile.fail()) {
std::cout << "Error has been detected when opening a file." << endl;
system("pause" );
return 1;
}
inFile.get(character);
while (!inFile.eof()) {
if (isalpha(character)) {
countAlpha++;
}
if (isdigit(character)) {
countNum++;
}
if (isupper(character)) {
countUpper++;
}
if (islower (character)) {
countLower++;
}
if (isspace (character)) {
countControl++;
}
if (ispunct (character)) {
countPunct++;
}
inFile.get(character);
}
std::cout << "Number of uppercase letters: " << setw(5) << countUpper << endl;
std::cout << "Number of lowercase letters: " << setw(5) << countLower << endl;
std::cout << "Number of numeric numbers: " << setw(5) << countNum << endl;
std::cout << "Number of alphabetic letters: " << setw(5) << countAlpha << endl;
std::cout << "Number of control letters: " << setw(5) << countControl << endl;
std::cout << "Number of punctuations: " << setw(5) << countPunct << endl;
std::cout << "Total numbers of the characters in the file: " << setw(5) << total << endl;
outFile.setf(ios::fixed);
outFile.setf(ios::showpoint);
outFile.precision(2);
std::cout << "Percentage of uppercase letters: " << setw(4) << countUpper / total << " %" << endl;
std::cout << "Percentage of lowercase letters: " << setw(4) << countLower / total << " %" << endl;
std::cout << "Percentage of numeric numbers: " << setw(4) << countNum / total << " %" << endl;
std::cout << "Percentage of alphabetic letters: " << setw(4) << countAlpha / total << " %" << endl;
std::cout << "Percentage of control letters: " << setw(4) << countControl / total << " %" << endl;
std::cout << "Percentage of punctuations: " << setw(4) << countPunct / total << " %" << endl;
outFile.close();
system("pause" );
return 0;
}
Last edited on Nov 6, 2016 at 5:41pm UTC
Nov 6, 2016 at 6:09pm UTC
The file should be in the same folder as your project. Also, can you post some lines from the file to check it's format?
Nov 6, 2016 at 7:35pm UTC
Yeah, my sample file is in the same folder. I just did a notepad document and wrote some numbers, letters, and punctuation to test it out.
Topic archived. No new replies allowed.