2015-05-09 78 views
0

我不知道如何解決它。它完成這項工作,一切看起來不錯,但在加密結束時崩潰。 這可能是最後一個標誌。轉錄密碼加密程序崩潰

#include <iostream> 
#include <math.h> 
#include <string> 
using namespace std; 

int main() 
{ 
    string text = "This is a sample text that i want to encrypt"; 
    int d = text.length(); 
    int n = ceil(sqrt(d)); 
    string encrypted[n * n]; 

    int i = 1; 
    int pos = 1; 

    while(i <= n) 
    { 
     int j = 0; 

     while(j < n) 
     { 
      encrypted[pos] = text[i + j * n - 1]; 
      cout << text[i + j * n - 1] << " "; 
      pos++; 
      j++; 
     } 

     cout << endl; 
     i++; 
    } 

    for(int i = 0; i < d; i++) 
     cout << encrypted[i]; 

    return 0; 
} 
+0

檢查超出範圍的訪問權限,並且'string encrypted [n * n]'是可變長度數組,它不受官方C++標準支持。改爲使用'std :: vector '。 – vsoftco

回答

0

的問題是存在的,你在哪裏,要求這樣的事情:

encrypted[pos] = text[i + j * n - 1]; 

其中:

  • ň
  • Ĵn和另外由ň

所以相乘,其結果是,你是請求單元N + N * N 文本,這是隻有d元素長。 從上面的情況看,索引56超過了44個。

+0

不,它會去n +(n-1)* n - 1。我不乘以j。 –