0

I have written a code, but there’s one Problem. When I put cout<<""<<endl; out, I have Bus error: 10. How can I solve this Problem. This is my code:

#include <iostream>
#include <sstream>
#include <cstdlib>
#include <cstdlib>
#include <cstring>
#include <cstdlib>
#include <cstring>
#include <assert.h>
#include <ctime>
#include <sstream>
#include <iostream>
#include <iomanip>
using namespace std;
char* rotate(char const* a, int b)
{
    char* r;
    for (int i = 0; i < int(strlen(a)); i++) {
        if (i >= int(strlen(a)) - b) {
            r[i] = a[i - int(int(strlen(a)) - b)];
        }
        else if (i + b < 0) {
            r[i] = a[int(strlen(a) + b)];
        }
        else {
            r[i] = a[i + b];
        }
    }
    r[int(strlen(a))] = '\0';
    return r;
}
char* Crypt(char* Input, bool Encrypt, int InitialRotation, int Rotation)
{
    char const* Table;
    try {
        Table = new char[size_t(strlen(Table))];
    }
    catch (...) {
        throw "No Memory";
    };
    char* a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    try {
        a = new char[size_t(strlen(a))];
    }
    catch (...) {
        throw "No Memory";
    };
    char* ActualTable;
    try {
        ActualTable = new char[size_t(strlen(ActualTable))];
    }
    catch (...) {
        throw "No Memory";
    };
    int Offset = InitialRotation;
    char* Result;
    try {
        Result = new char[size_t(strlen(Result))];
    }
    catch (...) {
        throw "No Memory";
    };
    int b;
    Table = "JPGVOUMFYQBENHZRDKASXLICTW";
    a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for (int i = 0; i < int(strlen(Input)); i++) {
        for (int c = 0; i < int(strlen(a)); c++) {
            if (a[c] == Input[i]) {
                b = c;
                break;
            }
        }
        cout << "" << endl;
        ActualTable = rotate(Table, Offset);
        Result[i] = rotate(Table, Offset)[b];
        Offset = Offset + Rotation;
    }
    return Result;
}
int main()
{
    char* A = "ZZZZ";
    cout << Crypt(A, 1, 1, 4) << endl;

    return 0;
}
rioV8
  • 24,506
  • 3
  • 32
  • 49
  • 2
    Not sure what exactly would cause the problem, but there is plenty of undefined behavior in your code. `char *Result; try { Result=new char[size_t(strlen(Result))]; }catch(...){ throw "No Memory"; };` and similar just don't make any sense. The size of `Result` is ... the string length of `Result`?? – Lukas-T Sep 08 '21 at 09:03
  • 3
    why are you using `strlen` here `Table=new char[size_t(strlen(Table))];` ? `Table` is uninitiailzed and the code has ub – 463035818_is_not_an_ai Sep 08 '21 at 09:03
  • 1
    And what use is e.g. `a = new char[size_t(strlen(a))]`? That makes no sense, assigning back to `a` itself, making you loose the original string. You do this many times, including with `Table = "JPGVOUMFYQBENHZRDKASXLICTW"`. – Some programmer dude Sep 08 '21 at 09:05
  • 1
    And besides it all, you also forget the string null-terminator in all your allocations. Why are you even using `char*` for your strings, instead of `std::string`? – Some programmer dude Sep 08 '21 at 09:06
  • 1
    `rotate` is also a buggy. You never allocate any memory to `r` but keep writing to it. Also `r[i] = a[int(strlen(a) + b)];` should be obviously wrong. Reading from `a` at `strlen(a)` will always yield the null-terminator `'\0'`, reading beyond it is undefined behavior. I suggest to get a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and learn the basics of C++. And make your life easier by not using pointers too much. – Lukas-T Sep 08 '21 at 09:11
  • 2
    Fix [all these warnings](https://godbolt.org/z/oq7oab6da) – Ted Lyngmo Sep 08 '21 at 09:15

0 Answers0