2013-03-11 162 views
0

好吧,所以我一直在寫任務來解密簡單的凱撒密碼。凱撒密碼解密器

我有一切正常工作,只有我的輸入停止約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; 
} 
+0

是的,您使用'sizeof'不正確。這裏不適用,使用'.length()'。你的其他錯誤是,你不檢查'isalpha'索引你的字母數組。可能會有更多的錯誤。 – 2013-03-11 06:39:15

+0

是的,我只是將它們移動到.length()。現在正在處理另一個錯誤。感謝您的反饋 – LeviTheDegu 2013-03-11 06:41:27

回答

1

您正在使用什麼編譯器。我,它不編譯,因爲

//打開文件 inputFile.open(inputFileName);

需要爲 //打開文件 inputFile.open(inputFileName.c_str());

http://www.cplusplus.com/reference/fstream/ifstream/open/

,但是當它編譯,事情似乎好的工作。整個文件被讀取。

+0

閱讀是的,但它在輸出提前停止。這似乎與我使用Sizeof(myarray)有關;但是,當我將它傳遞給myArray.Length()時,代碼將中斷:a06.exe中的0x013F828E處理的異常:0xC0000005:訪問衝突讀取位置0x002A4000。 – LeviTheDegu 2013-03-11 06:26:55

+1

sizeof(string)== sizeof(myarray)不管多大的字符串。你可能想使用myarray.length()。試試這個: – pkumar0 2013-03-11 06:36:04

+0

myarray.length = 1000 count_array [1000]導致訪問衝突。 – pkumar0 2013-03-11 06:42:50

1

decipher,sizeof(myArray)爲您提供了一個string實例的大小。 string實例可能包含字符串的實際長度,非常小的字符串的一些空間和實際數據的指針,但它不包含數據,即字符串本身。

而不是sizeof(myArray),請使用myArray.length()

+0

是的,我發佈了這個變化幾分鐘後,因爲我意識到我在做什麼,代碼只是沒有決定更新。 – LeviTheDegu 2013-03-11 06:53:54