好吧,所以我一直在寫任務來解密簡單的凱撒密碼。凱撒密碼解密器
我有一切正常工作,只有我的輸入停止約29個字符英寸它從一個非常大的文件中讀取,文本停在:「獨立宣言」中期句子。任何想法可能導致這種情況?我認爲我在濫用和濫用這裏的東西,隨意扔石頭在我的頭,我敢肯定,我可以使用更多的「學習」
編輯:經過進一步調查,我相信我的問題是要做我的循環和sizeof(myarray的)
編輯2:我編輯的代碼,現在它打破了,因爲「訪問衝突閱讀位置0x002A4000」:
for (int i = 0; i < myArray.length(); i++)
{
// pritns each occurance cout << char(i + 'a') << " has " << count_Array[i] <<" occuarnces" <<endl;
if (count_Array[i] > tester)
{
//finds largest
tester = count_Array[i];
max_array_value = i;
}
}
// print
Thanks for any tips :]
code:
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;
int decipher(string myArray, string outputFileName);
int main()
{
string outputFileName;
string inputFileName;
ifstream inputFile;
string reply;
char myArray;
string all_text = "";
//getting input and output files
cout << "Please input filename: ";
getline(cin,inputFileName);
cout <<"please enter output filename:";
getline(cin,outputFileName);
//opens file
inputFile.open(inputFileName);
if (!inputFile.is_open())
{
//file failed to open
cout <<"unable to open input file." <<endl << "Press enter to continue...";
getline(cin,reply);
exit(1);
}
//read file into all_text
while(inputFile.peek() != EOF)
{
inputFile.get(myArray);
all_text+=myArray;
}
// prints out file cout <<all_text;
inputFile.close();
//send to decipher
decipher(all_text, outputFileName);
cout << "press enter to continue";
getline(cin,reply);
return 0;
}
int decipher(string myArray, string outputFileName)
{
char default_alp[26] = {'a', 'b' ,'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };
int count_Array[26] = { 0 };
int tester = count_Array[0];
int max_array_value = 0;
string my_Message;
char temp;
ofstream outputFile;
//gets count of occurances
for (int i = 0; i < myArray.length(); i++)
{
count_Array[tolower(myArray[i]) - 'a']++;
}
for (int i = 0; i < myArray.length(); i++)
{
// pritns each occurance cout << char(i + 'a') << " has " << count_Array[i] <<" occuarnces" <<endl;
if (count_Array[i] > tester)
{
//finds largest
tester = count_Array[i];
max_array_value = i;
}
}
// prints useful information cout << "Largest number of occurances " << default_alp[max_array_value] <<endl << "shift amount is: " << default_alp[max_array_value]-'e' <<endl;
for (int i = 0; i < myArray.length(); i++)
{
//prints out each letter cout << myArray[i];
}
for (int i = 0; i < myArray.length; i++)
{
cout <<myArray.length();
//shifts the text based on value in dec
int shift = default_alp[max_array_value]-'e';
temp = myArray[i];
if (isalpha(temp))
{
if(tolower(myArray[i])-shift >= 97)
{
my_Message += tolower(myArray[i]) - shift;
}
else
{
my_Message += tolower(myArray[i]) + 26-shift;
}
}
else
{
my_Message +=myArray[i];
}
}
outputFile.open(outputFileName);
outputFile << my_Message;
return 0;
}
是的,您使用'sizeof'不正確。這裏不適用,使用'.length()'。你的其他錯誤是,你不檢查'isalpha'索引你的字母數組。可能會有更多的錯誤。 – 2013-03-11 06:39:15
是的,我只是將它們移動到.length()。現在正在處理另一個錯誤。感謝您的反饋 – LeviTheDegu 2013-03-11 06:41:27