Code:
#include <iostream>
#include <fstream>
#include <cctype>
#include <array>
using namespace std;
void counting(char ch, int list[]);
void copyText(ifstream& in,ofstream& out,char& ch,int list[]);
void initialize(int&,int[]);
void writeTotal(ofstream& out, int loc, int list[]);
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;
counting(ch,list);
in.get(ch);
}
out << ch;
}
void counting(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 writeTotal(ofstream& out, int loc, int list[])
{
out<<endl<<endl;
out<<"The number of line: "<< loc <<endl;
for (int i = 0; i<26; ++i) {
out<<static_cast<char>(i + static_cast<int>('A')) << " count = "<< list[i]<<endl;
}
}
int main()
{
int letterCount[26];
int lineCount;
char ch;
ofstream outfile;
outfile.open("/Users/peterjocham/Desktop/textoutput.txt",ios::out);
ifstream infile("/Users/peterjocham/Desktop/textinput.txt",ios::in);
initialize(lineCount, letterCount);
infile.get(ch);
while (infile) {
copyText(infile, outfile, ch, letterCount); //counts each line
lineCount++;
infile.get(ch);
}
writeTotal(outfile, lineCount, letterCount);
infile.close();
outfile.close();
return 0;
}
This what it is reading and below it is giving these numbers....
A is given me 0 and clearly this is not true. Line 40-47 is the error I think...
"I think it's fair to say that personal computers have become the most empowering tool we've ever created.They're tools of communication, they're tools of creativity, and they can be shaped by their user.
Bill Gates"
The number of line: 4
A count = 0
B count = 4
C count = 7
D count = 3
E count = 25
F count = 3
G count = 2
H count = 9
I count = 11
J count = 0
K count = 1
L count = 6
M count = 6
N count = 7
O count = 16
P count = 4
Q count = 0
R count = 11
S count = 10
T count = 20
U count = 3
V count = 4
W count = 2
X count = 0
Y count = 6
Z count = 0