HELP PLEASE

The problem I have is with the delete[]. If I just eliminate it from the function, it works. But when I run the code with the delete[], the program runs but then an error appears. How can I delete the memory that the "cifrada" and "descifrada" are using correctly? THANK YOU!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void main(){
	char alfabeto[] = {"abcdefghijklmnopqrstuvwxyz"};
	char numeros[] = {"0123456789"};
	char frase[256];
	cout<<"Introduzca la frase: ";
	cin.getline(frase,256);
	int l=strlen(frase);
	char *cifrada;
	char *descifrada;
	cifrada=new char[l];
	descifrada=new char[l];

	cifrar(alfabeto, numeros, frase,cifrada, l);
	cout<<"La frase cifrada es: "<<cifrada<<endl;
	descifrar(alfabeto, numeros, cifrada,descifrada, l);
	cout<<"La frase descifrada es: "<<descifrada<<endl;
	delete[] cifrada;
	delete[] descifrada;


THANK YOU!!
You probably did something wrong in the functions with those char arrays. That's why the delete's blow up. All I can guess at this point is that you haven't made the char arrays long enough since you didn't add 1 for the null terminator '\0' at the end.

If it's not that then you need to post your encrypt/decrypt functions.
This is the whole file. As I have said,without delete[] everything goes fine. Hope you can identidy the error now. Thank you !!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
using namespace std;

void cifrar(char *alfa, char *num, char *fr,char *cifrado,  int len);
void descifrar(char *alfa, char *num, char *cifrado,char *descifrado, int len);

void main(){
	char alfabeto[] = {"abcdefghijklmnopqrstuvwxyz"};
	char numeros[] = {"0123456789"};
	char frase[256];
	cout<<"Introduzca la frase: ";
	cin.getline(frase,256);
	int l=strlen(frase);
	char *cifrada;
	char *descifrada;
	cifrada=new char[l];
	descifrada=new char[l];

	cifrar(alfabeto, numeros, frase,cifrada, l);
	cout<<"La frase cifrada es: "<<cifrada<<endl;
	descifrar(alfabeto, numeros, cifrada,descifrada, l);
	cout<<"La frase descifrada es: "<<descifrada<<endl;
	delete[] cifrada;
	delete[] descifrada;
}


void cifrar(char *alfa, char *num, char *fr,char *cifrado,  int len){
	for(int d=0;d<len;d++){
		cifrado[d]=fr[d];
	}
	cifrado[len]='\0';
	for(int i=0;i<len;i++){
		for(int j=0;j<26;j++){
			if(fr[i]==alfa[j]){
				cifrado[i]=alfa[25-j];
				break;
			}
		}
	}
	for(int a=0;a<len;a++){
		for(int b=0;b<10;b++){
			if(fr[a]==num[b]){
				cifrado[a]=num[9-b];
				break;
			}
		}
	}
}


void descifrar(char *alfa, char *num, char *cifrado,char *descifrado, int len){
	for (int d=0;d<len;d++){
		descifrado[d]=cifrado[d];
	}
	descifrado[len]='\0';
	for(int i=0;i<len;i++){
		for(int j=0;j<26;j++){
			if(cifrado[i]==alfa[j]){
				descifrado[i]=alfa[25-j];
			}
		}
	}
	for(int a=0;a<len;a++){
		for(int b=0;b<10;b++){
			if(cifrado[a]==num[b]){
				descifrado[a]=num[9-b];
			}
		}
	}
}

I tried running your code and it gave me the error "strlen was not declared in this scope". Including the header <cstring> solved it.

By the way, if I can give you a piece of advice, code in English wherever possible. It makes it easier for people who don't know Spanish to read your code :)
Well, you didn't bother to fix the possible error that I mentioned. And I don't see anything else, so why not try:
1
2
3
    int l = strlen(frase);
    char *cifrada = new char[l + 1];            // + 1 for the '\0' at the end
    char *descifrada = new char[l + 1];



Here's another way to write it. Note that you only need the one function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cstring>
using namespace std;

void cifrar(char *fr,char *cifrado,int len);

int main(){
    char frase[256];
    cout << "Introduzca la frase: ";
    cin.getline(frase, 256);

    int l = strlen(frase);
    char *cifrada = new char[l + 1];
    char *descifrada = new char[l + 1];

    cifrar(frase, cifrada, l);
    cout << "La frase cifrada es: " << cifrada << '\n';
    cifrar(cifrada, descifrada, l);
    cout << "La frase descifrada es: " << descifrada << '\n';

    delete[] cifrada;
    delete[] descifrada;
}

void cifrar(char *fr, char *cifrado, int len)
{
    for (int i = 0; i < len; i++) {
        if (fr[i] >= 'a' && fr[i] <= 'z')
            cifrado[i] = 'z' - (fr[i] - 'a');
        else if (fr[i] >= 'A' && fr[i] <= 'Z')
            cifrado[i] = 'Z' - (fr[i] - 'A');
        else if (fr[i] >= '0' && fr[i] <= '9')
            cifrado[i] = '9' - (fr[i] - '0');
        else
            cifrado[i] = fr[i];
    }
    cifrado[len] = '\0';
}


Or you could actually write it in C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
using namespace std;

string encrypt(const string &phrase);

int main() {
    string phrase;
    cout << "Enter a phrase: ";
    getline(cin, phrase);

    string encrypted = encrypt(phrase);
    cout << "La frase cifrada es: " << encrypted << '\n';
    string decrypted = encrypt(encrypted);
    cout << "La frase descifrada es: " << decrypted << '\n';
}

string encrypt(const string &phrase) {
    string ret;
    for (size_t i = 0; i < phrase.size(); i++) {
        if (phrase[i] >= 'a' && phrase[i] <= 'z')
            ret += 'z' - (phrase[i] - 'a');
        else if (phrase[i] >= 'A' && phrase[i] <= 'Z')
            ret += 'Z' - (phrase[i] - 'A');
        else if (phrase[i] >= '0' && phrase[i] <= '9')
            ret += '9' - (phrase[i] - '0');
        else
            ret += phrase[i];
    }
    return ret;
}
Last edited on
Hi jaituk,

You could of course consider moving away from C style programming by doing the following:

* Use std::string instead of char arrays. It has an [] operator, one can use it in the same way
* Don't use new or delete - the STL containers do memory management efficiently and well.

By the way, the main function returns an int, not void, it is in the standard.

Good Luck
fill_n (&cifrada, (int of the size of cifrada), '\0'); I think. check later.
Last edited on
Topic archived. No new replies allowed.