【发布时间】:2013-02-26 06:02:06
【问题描述】:
好吧,所以我一直“受命”编写一个程序来解密简单的凯撒密码。
我的一切工作正常,只有我的输入停止了大约 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。可能还有更多错误。 -
是的,我只是将它们移至 .length()。现在处理另一个错误。感谢您的反馈
标签: c++ encryption