Need help, been stuck on this the past couple days i dont really understand how to get my code to read from my file and count each occurence of a letter wheter they are uppr or lower case and output them to a file, just need help with the counting part, have to do it all through a function.

Here is my source code:

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void getData(ifstream& inPara, ofstream& outChara, char texT[], int numLet[]);

int main()
{
    ifstream inFile;
    ofstream outFile;
    char ch[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    int numofCharacters[26] = {0};
    char target;

    inFile.open("Lab7.txt");//opens input file

    if (!inFile)//runs error if inFile could not be opened
    {
        cout << "Error unable to open Input File" << endl;
        cout << "Program Terminated!" << endl;
        return 1;
    }

    outFile.open("Output.txt");//opens Output File

    if (!outFile)//runs error if Output File could not be opened
    {
        cout << "Error unable to open Output File" << endl;
        cout << "Program Terminated!" << endl;
        return 1;
    }
    while (!inFile.eof())//runs program untile end of file
    {
        inFile >> target
    }

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

    return 0;
}

void getData(ifstream& inPara, ofstream& outChara, char texT[], int numLet[])
{

    for (int i = 0; i < 26; i++) {
    if (texT[i]) {
        numLet[i]++;
        }
    }
    for (int i = 0; i < 26; i++) {
        cout << numLet[i] << " " << endl;
    }
}

Recommended Answers

All 3 Replies

Breaking down the process into simple functions, enables you to figure out where you are going wrong. Here, this example creates x amount of random letters in a file, Lab7.txt, and then reads the file, counting the letters. Then displaying the counts and total.

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main()
{
    ofstream outFile("Lab7.txt");
    int a = 0; int A = 0;
    int b = 0; int B = 0;
    int c = 0; int C = 0;
    int d = 0; int D = 0;
    int e = 0; int E = 0;
    int f = 0; int F = 0;
    int g = 0; int G = 0;
    int h = 0; int H = 0;
    int i = 0; int I = 0;
    int j = 0; int J = 0;
    int k = 0; int K = 0;
    int l = 0; int L = 0;
    int m = 0; int M = 0;
    int n = 0; int N = 0;
    int o = 0; int O = 0;
    int p = 0; int P = 0;
    int q = 0; int Q = 0;
    int r = 0; int R = 0;
    int s = 0; int S = 0;
    int t = 0; int T = 0;
    int u = 0; int U = 0;
    int v = 0; int V = 0;
    int w = 0; int W = 0;
    int x = 0; int X = 0;
    int y = 0; int Y = 0;
    int z = 0; int Z = 0;


    string letters[52] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    for(int i = 0; i < 10240; i++)
    {  
        int r = rand() % 52;
        letters[r];
     //   std::cout << letters[r] << "";
        outFile << letters[r] << "";
        outFile << "\n";
    }
    outFile.close();

    char target = ' ';
    ifstream inFile("Lab7.txt");
    while (inFile >> target)
    {
        if (target == 'a')
            a++;
        if (target == 'b')
            b++;
        if (target == 'c')
            c++;
        if (target == 'd')
            d++;
        if (target == 'e')
            e++;
        if (target == 'f')
            f++;
        if (target == 'g')
            g++;
        if (target == 'h')
            h++;
        if (target == 'i')
            i++;
        if (target == 'j')
            j++;
        if (target == 'k')
            k++;
        if (target == 'l')
            l++;
        if (target == 'm')
            m++;
        if (target == 'n')
            n++;
        if (target == 'o')
            o++;
        if (target == 'p')
            p++;
        if (target == 'q')
            q++;
        if (target == 'r')
            r++;
        if (target == 's')
            s++;
        if (target == 't')
            t++;
        if (target == 'u')
            u++;
        if (target == 'v')
            v++;
        if (target == 'w')
            w++;
        if (target == 'x')
            x++;
        if (target == 'y')
            y++;
        if (target == 'z')
            z++;
        if (target == 'A')
            A++;
        if (target == 'B')
            B++;
        if (target == 'C')
            C++;
        if (target == 'D')
            D++;
        if (target == 'E')
            E++;
        if (target == 'F')
            F++;
        if (target == 'G')
            G++;
        if (target == 'H')
            H++;
        if (target == 'I')
            I++;
        if (target == 'J')
            J++;
        if (target == 'K')
            K++;
        if (target == 'L')
            L++;
        if (target == 'M')
            M++;
        if (target == 'N')
            N++;
        if (target == 'O')
            O++;
        if (target == 'P')
            P++;
        if (target == 'Q')
            Q++;
        if (target == 'R')
            R++;
        if (target == 'S')
            S++;
        if (target == 'T')
            T++;
        if (target == 'U')
            U++;
        if (target == 'V')
            V++;
        if (target == 'W')
            W++;
        if (target == 'X')
            X++;
        if (target == 'Y')
            Y++;
        if (target == 'Z')
            Z++;
     //   std::cout <<  target;

    }
    std::cout << "\n";
    std::cout << "a:" << a << "  " << "A:" << A << std::endl;
    std::cout << "b:" << b << "  " << "B:" << B << std::endl;
    std::cout << "c:" << c << "  " << "C:" << C << std::endl;
    std::cout << "d:" << d << "  " << "D:" << D << std::endl;
    std::cout << "e:" << e << "  " << "E:" << E << std::endl;
    std::cout << "f:" << f << "  " << "F:" << F << std::endl;
    std::cout << "g:" << g << "  " << "G:" << G << std::endl;
    std::cout << "h:" << h << "  " << "H:" << H << std::endl;
    std::cout << "i:" << i << "  " << "I:" << I << std::endl;
    std::cout << "j:" << j << "  " << "J:" << J << std::endl;
    std::cout << "k:" << k << "  " << "K:" << K << std::endl;
    std::cout << "l:" << l << "  " << "L:" << L << std::endl;
    std::cout << "m:" << m << "  " << "M:" << M << std::endl;
    std::cout << "n:" << n << "  " << "N:" << N << std::endl;
    std::cout << "o:" << o << "  " << "O:" << O << std::endl;
    std::cout << "p:" << p << "  " << "P:" << P << std::endl;
    std::cout << "q:" << q << "  " << "Q:" << Q << std::endl;
    std::cout << "r:" << r << "  " << "R:" << R << std::endl;
    std::cout << "s:" << s << "  " << "S:" << S << std::endl;
    std::cout << "t:" << t << "  " << "T:" << T << std::endl;
    std::cout << "u:" << u << "  " << "U:" << U << std::endl;
    std::cout << "v:" << v << "  " << "V:" << V << std::endl;
    std::cout << "w:" << w << "  " << "W:" << W << std::endl;
    std::cout << "x:" << x << "  " << "X:" << X << std::endl;
    std::cout << "y:" << y << "  " << "Y:" << Y << std::endl;
    std::cout << "z:" << z << "  " << "Z:" << Z << std::endl;

    int total = 0;
    total = a + b + c + d + e + f + g + h + i + j + k + l + m + 
            n + o + p + q + r + s + t + u + v + w + x + y + z + 
            A + B + C + D + E + F + G + H + I + J + K + L + M + 
            N + O + P + Q + R + S + T + U + V + W + X + Y + Z;
    std::cout << "total: " << total;
    inFile.close();
    return 0;
}

a:196 A:183
b:196 B:157
c:207 C:192
d:185 D:206
e:197 E:199
f:191 F:197
g:183 G:171
h:223 H:216
i:193 I:193
j:201 J:191
k:218 K:194
l:192 L:203
m:183 M:207
n:171 N:192
o:194 O:217
p:211 P:202
q:198 Q:201
r:196 R:197
s:204 S:181
t:197 T:223
u:212 U:185
v:199 V:192
w:196 W:210
x:181 X:213
y:190 Y:184
z:211 Z:209
total: 10240

commented: Bad code and does not help OP -3

DO you have any explanation for creating 52 variables, 52 if tests etc when a simple array or a map<char, int> would be so much better?
And why use strings to hold single chars?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.