首页 > 解决方案 > 试图做书密码。内存错误(访问冲突)

问题描述

我正在尝试做一本书密码。我生成一个随机数向量,然后把它放到表中,然后我就有了我的加密算法。当我将算法放入 cout 时,它不起作用 - 它向我显示正确加密的数字。当我尝试将它移动到主函数中的 int 指针时,它会显示内存错误。

#include <iostream>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <vector>
#include <ctime>

using namespace std;

void generate( int tab[26][26] )
{
        int h;
        vector<int> table;
        for(int i = 0; i<676; i++)
        {
                table.push_back(i+1);
        }
        int g = 0;
        random_shuffle(table.begin(), table.end());
        for(int x = 0; x < 26; x++)
        {
                for(int y = 0; y < 26; y++)
                {
                        tab[x][y] = table[g];
                        g++;
                }
        }
}

void print_cipher_table(int tab[26][26])
{
        char sign = 'A';
        cout << "    ";
        for (int n = 0; n<26; n++)
        {
                cout <<  sign++ << "   ";
        }
        cout << "\n";
        sign = 'A';
        for(int x = 0; x<26; x++)
        {
                cout << sign++ << "  ";
                for(int y = 0; y<26; y++)
                {
                        if(tab[x][y] < 10) cout << "00" << tab[x][y] << " ";
                        else if(tab[x][y] < 100) cout << "0" << tab[x][y] << " ";
                        else cout << tab[x][y] << " ";
                }
                cout << "\n";

        }
}

void encrypting(int tab[26][26], string s_true, string s_false, int *result)
{
        int row, column, i=0;

        while(s_true[i])                // pêtla wykonuje siê dopóki nie napotka na koniec stringu
        {
                if(s_true[i]>64 && s_true[i]<91)
                {
                        row = s_true[i] -'A';
                        column = s_false[i] - 'A';
                }
                else {
                        cout << "NIE PODAŁEŚ WIELKICH LITER!" << endl;
                        exit(0);
                }

                cout << "\t" << tab[row][column];
                result[i] = tab[row][column];
                i++;
        }
}

int main()
{
        int tab[26][26], slength, *crypted;
        string s_true, s_false;
        string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        cout << "Prosze podac tekst prawdziwy: ";
        cin >> s_true;
        cout << "Prosze podac tekst falszywy: ";
        cin >> s_false;
        if(s_true.length() != s_false.length())
        {
                cout << "Chujowa dlugosc tekstu. Wypierdalaj";
                exit(1);
        }
        slength = s_true.length();
        generate( tab );
        print_cipher_table( tab );
        cout << "\n\n\n";
        encrypting(tab, s_true, s_false, crypted);
        cout << *crypted;
        return 0;
}

标签: c++encryptionmemory

解决方案


推荐阅读