-2

I have an error in my code that encrypts and decrypts* a four digit number. It adds 12 to each number in the array then swaps the 1st with the 3rd,swaps the 2nd with the 4th to give me the final answer. The error am getting when i select option 2 after compiling is "Run-Time Check Failure #2 - Stack around the variable 'e' was corrupted." which i don't understand. Please help

#include <iostream>

using namespace std;

int input;
int one;
int two;
int three;
int four;

int p;
int o; 
int i;
int u;

int encryption(int one, int two, int three, int four);


int main() {
    main:

    cout << "Press 1 to Encrypt\n";
    cout << "Press 2 to Decrypt \n";
    cout << "Press 3 to Exit \n\n";
    cin >> input;

    while (input != 5) {
        break;
    }

    switch (input) {
    case 1:
        goto Encryp;
        break;
    case 2:
        goto Decryption;
        break;
    default:
        break;
    }
    Encryp:


    cout << "Enter the number to encrypt: ";
    cin >> p;
    cin >> o;
    cin >> i;
    cin >> u;

    one = p;
    two = o;
    three = i;
    four = u;

    encryption(one, two, three, four);

    cout << one << two << three << four;

    goto main;

    Decryption:
    //will be implimented after the fix



    getchar();
    getchar();

}

int encryption(int one, int two, int three, int four) {

    //int q, w, r, t;
    int swap1;
    int swap2;
    int e[3];

    e[0] = one;
    e[1] = two;
    e[2] = three;
    e[3] = four;

    e[0] = e[0] + 12;
    e[1] = e[1] + 12;
    e[2] = e[2] + 12;
    e[3] = e[3] + 12;


    //Swap the 1st with the 3rd
    swap1 = e[2]; 
    swap2 = e[3];

    e[2] = e[0]; 
    e[0] = swap1;

    e[3] = e[1];
    e[1] = swap2;

    one = e[0];
    two = e[1];
    three = e[2];
    four = e[3];

    return one, two, three, four;
}
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Willie Mwewa
  • 341
  • 3
  • 13
  • `goto Decryption;` `goto Encryp;` Call functions, don't use `goto`. Also `return one, two, three, four;` Explain what this is supposed to do. Have you ever seen a `return` statement look like this in any of the C++ books or materials you're using? – PaulMcKenzie Apr 20 '15 at 21:15
  • Wanted return one, two, three, four to return all the elements that where in the array. – Willie Mwewa Apr 20 '15 at 21:18
  • `goto main;` Your code is becoming spaghetti-ized, if not already. – PaulMcKenzie Apr 20 '15 at 21:24
  • 2
    Please do more reading on C++. That `return` statement does not return 4 values, a return statement can only return 1 value. You inadvertantly used the comma operator, which results in the last value being returned only: http://stackoverflow.com/questions/54142/how-does-the-comma-operator-work http://en.wikipedia.org/wiki/Comma_operator – PaulMcKenzie Apr 20 '15 at 21:27
  • @WillieMwewa `switch` mixed with `goto`, awwww that's burning my eyes. It's giving me physical pain bub!! Please don't do such things. If you want to structure your code, use functions please. – πάντα ῥεῖ Apr 20 '15 at 21:35

1 Answers1

1

You are declaring:

int e[3];

but going out of bounds:

e[3] = four;

this is the fourth index, and you only allocated 3 for the array. You need:

int e[4];

There are definitely some major problems with your code:

  • You are using unnecessary labels - you really should avoid goto statements at all costs.

  • Your return value from your encryption function makes no sense - you can't return multiple values, and you aren't even using the value that you return.

dwcanillas
  • 3,562
  • 2
  • 24
  • 33