So I need to decrypt a file, and my first step is to find the most common letter. I've written the following function to increment the letter count and it seems to be broken. Obviously this has yet to determine the largest element of the array, but that part should be easy once the first part is done.
Well, I recommend looking back at the documentation for switches. You'll plainly see what is wrong with your switch, though I'll leave the actual discovery of what's wrong to you.
So I've boiled it down to something similar to your suggestion, but my ending cout statment still results in an output of a long list of numbers, which seem to cycle up to four and five then repeat. The program exits in under ten seconds. Pausing the program after the first cout results in "00".
void count(int letters[26]) {
//declare variables
ifstream infile;
char fileName[151]; // allow for a very long file location
int index;
char ch;
cout << "Please enter the directory location of the file you wish to decrypt: " << endl;
cin >> fileName;
infile.open(fileName); // open the encrypted file
while(!infile.eof()) {
infile.get(ch);
ch = toupper(ch);
index = static_cast<int>(ch)
- static_cast<int>('A');
if (0 <= index && index < 26)
++letters[index];
cout << letters[21];
}
}
Is it possible something is wrong with the file input? To me the function looks solid.