I'm trying to write a program for a Caesar Cipher, and right now I'm working on the function to find the key to shift the cipher.
Now the problem arises that while it reads the file, the program breaks and I get the error:
"Exception thrown at 0x89012914 in ConsoleApplication11.exe: 0xC0000005: Access violation executing location 0x89012914. If there is a handler for this exception, the program may be safely continued."
Here's the code I have so far, is there anything obvious that I'm overlooking?
int findKey(string& file);
int main()
{
string inputFileName;
cout << "Input file name: ";
getline(cin, inputFileName);
findKey(inputFileName);
}
int findKey(string& file)
{
string reply;
ifstream inFile;
char character;
int count[26] = { 0 };
int nGreatest = 0;
inFile.open(file.c_str());
if (!inFile.is_open())
{
cout << "Unable to open input file." << endl;
cout << "Press enter to continue...";
getline(cin, reply);
exit(1);
}
while (inFile.peek() != EOF)
{
inFile.get(character);
cout << character;
if (int(character) >= 65 || int(character) <= 90)
{
count[(int(character)) - 65]++;
}
else if (int(character) >= 97 || int(character) <= 122)
{
count[(int(character)) - 97]++;
}
}
for (int i = 0; i < 26; i++)
{
if (count[i] > nGreatest)
nGreatest = count[i];
}
cout << char(nGreatest) << endl;
return 0;
}