Program that counts number of characters

Hi guys, can't find a solution. Tha program compiles perfectly, but I don't have the correct output: number of letters and lines. Possibly I messed up with while loop. I will appreciate any help, thanks!

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
  #include <iostream>
#include <fstream>
#include <cctype>


using namespace std;

void initialize(int&, int[]);
void copyText (ifstream&, ofstream&, char&, int[]);
void count(char, int[]);
void print(ofstream&, int, int[]);

int main()
{ int line;
    int letter[26];
    char ch;
    ifstream infile;
    ofstream outfile;
    infile.open("input.txt");
    outfile.open("output21.txt");
    initialize(line, letter);
    infile.get(ch);
    while (infile)
    {copyText(infile, outfile, ch, letter);
        line++;
        infile.get(ch);}
        print (outfile, line, letter);
    infile.close();
    outfile.close();
    return 0;
}
void initialize(int& loc, int list[])
{loc = 0;
for (int i =0; i < 26; i++)
list[i] = 0;
}

void copyText(ifstream& in, ofstream& out, char& ch, int list[])
{while (ch != '\n')
{out << ch;
    count(ch, list);
    in.get(ch);
}
    out << ch;
}
void count(char ch, int list[])
{ ch = toupper(ch);
    int index = static_cast<int>(ch)-static_cast<int>('A');
    if (0 <= index && index < 26)
        list[index]++;
}
void print (ofstream& out, int loc, int list[])
{
    out << endl<< endl;
    out << " The number of lines is = "<<loc <<endl;
    for (int i = 0; i<26; i++)
    out << static_cast<char>(i+static_cast<int>('A')) << " count =  " <<list[i] << endl;
}
Topic archived. No new replies allowed.