Input File Statistics

I have been trying (for 3 days now) to write a code that will return the statistics of an input file. The function needs to returns the following statistics of the input.txt file. Also add the percentage. (Platform: C++)
-        Total of characters processed (alpha + numeric + special character)
-        Total of digits processed
-        Total of Upper case letters processed (A-Z)
-        Total of hexadecimal digits processed
-        Total of Punctuation characters processed (, . ; ’ : ; )
-        Total of Control character processed
-        Total of lower case letters processed (a-z)
-        Total of all alphanumeric characters processed (A-Z, a-z, and 0-9)
-        Total of all printable characters
-        Total of blank spaces

What am I doing wrong? Any clue please? I have no idea how to make this happened. I don't know what I am doing wrong or what am I missing.

Thank you kindly,

Tired, sleep depraved, and frustrated.... :(

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
  
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cctype>
#include <string>

using namespace std;


void initialize(int& lc, int list[]);
void copyText(ifstream& intext, ofstream& outtext, char& ch, int list[]);
void characterCount(char ch, int list[]);
void writeTotal(ofstream& outtext, int lc, int list[]);

void characterSTATS(char ch, int list[]);
void writeSTATS(ofstream& outtext, int lc, int list[]);

int main()
{
 ///Variable declaration

    ifstream inFile;    //input file stream variable
    ofstream outFile;   //output file stream variable
    char inputFile[51]; //variable to store the name of the input file
    char outputFile[51];    //variable to store the name of the output file

    //Character Occurence
    int lineCount; //stores the line count
    int letterCount[26]; //store the letter count A-Z...0-26
    char ch; //store the characters

 ///Open the I/O files

    cout << "Enter the input file name: ";
    cin >> inputFile;
    cout << endl;
    inFile.open(inputFile);
    if (!inFile)
    {
        cout << "Cannot open the input file." << endl;
        return 1;
    }

    cout << "Enter the output file name: ";
    cin >> outputFile;
    cout << endl;
    outFile.open(outputFile);


    outFile <<endl;
    outFile <<"Original Message Contents"<<endl;
    outFile <<"---------------------------"<<endl;
    initialize(lineCount, letterCount);             //initialize lineCount & letterCount


    inFile.get(ch);                                 //read the first character
    while (inFile)
    {
        copyText(inFile, outFile, ch, letterCount); //process the lines
        lineCount++;                                //increment the line count
        inFile.get(ch);                             //read the next character
    }


    writeTotal(outFile, lineCount, letterCount);    //output # of lines and Char occurrence


    writeSTATS(outFile, lineCount, letterCount);




    inFile.close();
    outFile.close();

    return 0;
}

void initialize(int& lc, int list[]) //initialize lineCount & letterCount to 0
{
    int j;
    lc = 0;

    for (j = 0; j < 26; j++)
        list[j] = 0;
}



void copyText(ifstream& intext, ofstream& outtext, char& ch, int list[]) //outputs Original message contents
{

    while (ch != '\n')      //process the entire line
    {
        outtext << ch;      //output the character

        characterCount(ch, list);   //call the function character count
        intext.get(ch);     //read the next character
    }
    outtext << ch;          //output the newline character
}



void characterCount(char ch, int list[]) //counting character occurrence
{
    int index;

    ch = toupper(ch);                       //convert letter to uppercase

    index = static_cast<int>(ch)
            - static_cast<int>('A');        //Find the index array for this letter

    if (0 <= index && index < 26)           //IF the index is valid, add it to the appropriate count
        list[index]++;
}

void characterSTATS(char ch, int list[]){
  //  int index;


   // ch = toupper(ch);                       //convert letter to uppercase

  //  index = static_cast<int>(ch)- static_cast<int>('A');        //Find the index array for this letter

    if (0 <= ch && ch < 26)           //IF the index is valid, add it to the appropriate count
        list[ch]++;


    if (ch<='9'&& ch>='0')
    {
        list[0]++;
    }
    else if(ch>= 'A' && ch<= 'Z')
    {
        list[1]++;
    }
    else if(ch==',' && ch=='.'&& ch==';'&& ch=='"'&& ch==':')
    {
        list[3]++;
    }
    else if(ch=='>' && ch=='<'&& ch=='='&& ch=='!')
    {
        list[4]++;
    }
    else if(ch<='z' && ch>='a')
    {
        list[5]++;
    }
        else if(ch==' ' && ch=='A')
    {
        list[8]++;
    }

}

void writeTotal(ofstream& outtext, int lc, int list[]) //outputs to output file. line & letter count
{
    int index;
    char inputFile, outputFile, ch;

    outtext << endl << endl;
    outtext <<"Message Report"<<endl;
    outtext <<"---------------------------"<<endl;
    outtext <<" * Name: "<<endl;
    outtext <<" * Input File Name : "<<inputFile<<endl;
    outtext <<" * Output File Name: "<<outputFile<<endl;
    outtext <<" * Number of lines = " << lc << endl;
    outtext <<" * Number of chars = " << ch << endl;
    outtext <<" * Five Most Used Characters"<<endl;

    outtext <<" * Characters Not Used: "<<endl<<endl;
    outtext <<"____________________________________________"<< endl;
    outtext <<"Character Occurrence"<<endl;
    outtext <<"---------------------------"<<endl;


    for (index = 0; index<26; index = index + 3){
            if(index==23)

            outtext <<static_cast<char>(index+static_cast<int>('A'))
            <<" count= "<< list[index]<<"\t"
            <<static_cast<char>(index+static_cast<int>('A'))
            <<" count= "<<list[index+1]<<endl;
    else
        outtext<<static_cast<char>(index+static_cast<int>('A'))
        <<" count= "<< list[index]<<"\t"

        <<static_cast<char>((index+1)+static_cast<int>('A'))
        <<" count= "<<list[index+1]<<"\t"

        <<static_cast<char>((index+2)+static_cast<int>('A'))
        <<" count= "<<list[index+2]<<endl;
    }

    outtext <<"____________________________________________"<< endl;
}

void writeSTATS(ofstream& outtext, int lc, int list[]){
    int index;
    list[6] = list[5] + list[1] + list[0];
    int sum = list[7] = list[0] + list[1] + list[2] + list[3] + list[4] + list[5] + list[6] + list[7] + list[8];
    list[7] = sum - list[8];
    string type[9] = {"Digits", "UpperCase","HexDigit ", "Punctuation","Control","LowerCase","AlpahNumer", "Printable","Space"};

    outtext <<"Character Stats: "<<"total chars processed."<<endl;
    outtext <<"------------------------------------------"<<endl;
    outtext <<"TYPE         COUNT     PERCENT"<<endl;
    for(index = 0;index<9;index++)
        outtext <<std::left<<setw(10)<<type[index]<<setprecision(3)
                <<std::right<<setw(16)<<list[index]
                <<(list[index]/sum)*100 <<" %"<<endl;
        outtext <<setw(10)<<list[0]<<endl;
        outtext <<setw(10)<<list[1]<<endl;
        outtext <<setw(10)<<list[2]<<endl;
        outtext <<setw(10)<<list[3]<<endl;
        outtext <<setw(10)<<list[4]<<endl;
        outtext <<setw(10)<<list[5]<<endl;
        outtext <<setw(10)<<list[6]<<endl;
        outtext <<setw(10)<<list[7]<<endl;
        outtext <<setw(10)<<list[8]<<endl;
        outtext <<setw(10)<<list[index]<<endl;
        outtext <<"____________________________________________"<< endl;

 }

My feeling is that you may be trying too hard to deliver a Rolls-Royce solution which could be much more than is wanted. For example, you are counting the number of lines, and individual counts for each letter of the alphabet.

However, reading the original question, quote,
The function needs to returns the following statistics of the input.txt file. Also add the percentage. (Platform: C++)
- Total of characters processed (alpha + numeric + special character)
- Total of digits processed
- Total of Upper case letters processed (A-Z)
- Total of hexadecimal digits processed
- Total of Punctuation characters processed (, . ; ’ : ; )
- Total of Control character processed
- Total of lower case letters processed (a-z)
- Total of all alphanumeric characters processed (A-Z, a-z, and 0-9)
- Total of all printable characters
- Total of blank spaces

I don't think it's a coincidence that there are built-in functions to identify all of those:
1
2
3
4
5
6
7
8
9
    isdigit()
    isupper()
    isxdigit()
    ispunct()
    iscntrl()
    islower()
    isalnum()
    isprint()
    isspace()

See http://www.cplusplus.com/reference/cctype/

I think all you need to do is something like this:
1
2
3
4
5
6
7
    char ch;
                         
    while ( inFile.get(ch))
    {
        // use built-in function to test character ch
        // and increment a total for that category
    }


And then output the results.
I reworked my code, is working now. Thank you for the direction, really appreciate.
Topic archived. No new replies allowed.