我正在嘗試編寫一個凱撒密碼的程序,現在我正在尋找移動密碼的關鍵函數。問題雖然通過函數讀取文件
現在出現的問題是,同時在讀取文件時,程序中斷和我得到的錯誤:
「異常在ConsoleApplication11.exe在0x89012914拋出:0000005:訪問衝突執行位置0x89012914 如果有是這個例外的處理程序,程序可以安全地繼續。「
這是我到目前爲止的代碼,有沒有什麼明顯的我可以忽略?
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;
}
[看看這個](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Rakete1111