2015-05-09 82 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。 –