hey guys,
i am trying to get my code to count the number of characters in a file. However, the program is continuously going through the characters (so it goes from a-z thousands of times) when i want it to say how many times 'a' occured and 'b' and so on, in the text file.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void characterCount(char ch, int list[]);
void writeTotal(int list[]);
void initialize(int list[]);
int main()
{
int letterCount[26];
string fileName;
ifstream inStream;
inStream.open("C:\\data.txt");
if (inStream.fail())
{
cout << "could not open input file";
return 1;
}
while (!inStream.eof())
{
writeTotal(letterCount);
}
system("PAUSE");
return 0;
}
void initialize(int list[])
{
for(int j=0; j<26; j++)
list[j]=0;
}
void characterCount(char ch, int list[])
{
int index;
ch=tolower(ch);
index=static_cast<int>(ch)-static_cast<int>('a');
if (0 <= index & index <26)
list[index]++;
}
void writeTotal(int list[])
{
int index;
for(index=0; index<26; index++)
cout<<static_cast<char>(index
+static_cast<int>('a'))
<<"("<<list[index]<<")"<<endl;
}
When i change my while loop to:
while(!inStream.eof())
{
inStream>>fileName;
cout<<fileName<<" ("<<fileName.size()<<")"<<endl;
}
i manage to get all the words to display with the amount of characters in each word. I am struggling to make the transition from calculating how many in a word to how many of each letter.
Any help will be appreciated.