C ++ fix problem

1

I am entering by passing a char to an array of char and when inserting it does it well, as I enter and print everything from little to little.

Then I made another for to show only the arrangement and it turns out that only the last 5 letters of my char awaits me in everything.

Is that the fix is not maintained. I mean I put information and when I print the fix I get altered the arrangement, 2% range for : One adds information to the array and prints the array every cycle and the other for that is dedicated to printing the same array prints me very different, being the case that the 2 are the same arrangement.

I do not know what's wrong.

void Agregar(char Frase[], int Caracteres);
int main()
{
    string Texto;
    string Texto2;
    char Frase[180];

    cout<<"Ingresa La Palabra A Encriptar: ";
    getline(cin,Texto);

    for(int  i = 0; Texto[i] != 0;++i){
        if(Texto[i] != 32)
            Texto2 = Texto2 + Texto[i];
    }
    cout<<"Sin Espacios: "<<Texto2<<endl;

    int Num = Texto2.length();
    strcpy(Frase, Texto2.c_str());
    int CaracteresRest = 5-(Num%5);
    if (CaracteresRest==5)
    {
        CaracteresRest=0;
    }
    Agregar(Frase,CaracteresRest);
    Num = strlen(Frase);
    int Multiplo = Num/5;
    cout<<"Con *: "<<Frase<<endl;
    cout<<"**Arreglo**"<<endl;

    string * arreglo;
    arreglo = new string[Multiplo,5];
    string *arreglo2 = new string[Multiplo,5];

    int ContTex = 0;
    for(int i = 0; i < Multiplo; i++){
        for(int j = 0; j <5; j++){
            arreglo[i,j] = Frase[ContTex];
            ContTex = ContTex + 1;
            cout<< arreglo[i,j]<<" ";
        }
        cout<<""<<endl;
    }
//    delete [] arreglo;
/**El arreglo que imprimo aqui es muy diferente al paso y eso no deberia de pasar**/
    cout<<""<<endl;
    for(int i = 0; i < Multiplo; i++){
        for(int j = 0; j <5; j++){
            cout<< arreglo[i,j]<<" ";
        }
        cout<<""<<endl;
    }



}

void Agregar(char Frase[], int Caracteres){
    int i=0;
    while (Frase[i]!='
void Agregar(char Frase[], int Caracteres);
int main()
{
    string Texto;
    string Texto2;
    char Frase[180];

    cout<<"Ingresa La Palabra A Encriptar: ";
    getline(cin,Texto);

    for(int  i = 0; Texto[i] != 0;++i){
        if(Texto[i] != 32)
            Texto2 = Texto2 + Texto[i];
    }
    cout<<"Sin Espacios: "<<Texto2<<endl;

    int Num = Texto2.length();
    strcpy(Frase, Texto2.c_str());
    int CaracteresRest = 5-(Num%5);
    if (CaracteresRest==5)
    {
        CaracteresRest=0;
    }
    Agregar(Frase,CaracteresRest);
    Num = strlen(Frase);
    int Multiplo = Num/5;
    cout<<"Con *: "<<Frase<<endl;
    cout<<"**Arreglo**"<<endl;

    string * arreglo;
    arreglo = new string[Multiplo,5];
    string *arreglo2 = new string[Multiplo,5];

    int ContTex = 0;
    for(int i = 0; i < Multiplo; i++){
        for(int j = 0; j <5; j++){
            arreglo[i,j] = Frase[ContTex];
            ContTex = ContTex + 1;
            cout<< arreglo[i,j]<<" ";
        }
        cout<<""<<endl;
    }
//    delete [] arreglo;
/**El arreglo que imprimo aqui es muy diferente al paso y eso no deberia de pasar**/
    cout<<""<<endl;
    for(int i = 0; i < Multiplo; i++){
        for(int j = 0; j <5; j++){
            cout<< arreglo[i,j]<<" ";
        }
        cout<<""<<endl;
    }



}

void Agregar(char Frase[], int Caracteres){
    int i=0;
    while (Frase[i]!='%pre%')
    {
        ++i;
    }
    int TamTotal=i+Caracteres;
    while (i<TamTotal)
    {
        Frase[i]='*';
        ++i;
    }
    Frase[i]='%pre%';
}
') { ++i; } int TamTotal=i+Caracteres; while (i<TamTotal) { Frase[i]='*'; ++i; } Frase[i]='%pre%'; }
    
asked by Eduardo Cortez 02.11.2018 в 21:18
source

1 answer

2

These reservations are wrong:

arreglo = new string[Multiplo,5];
string *arreglo2 = new string[Multiplo,5];

The operator new has, according to the documentation , the following syntax:

void* operator new[] (std::size_t size);
void* operator new[] (std::size_t size, const std::nothrow_t& nothrow_value) noexcept;
void* operator new[] (std::size_t size, void* ptr) noexcept;

And as you see, there is no matching call, except for the third option, and you would not want that to happen because this option does not reserve memory , but it initializes the objects in ptr , which in your case is 5 .

new does not allow to make matrix reservations directly. To do this you have two options:

  • You make a single reservation of " rows * columns " elements and manage that region as an array. In this case you have to transform the linear region into one of two dimensions:

    char* array= new [Multiplo*5];
    
    char* elemento = &array[i*Multiplo];
    
  • You make two reservations. In this case the initialization is more complex but the use is simpler:

    char ** array= new char*[filas];
    for( int i=0; i<Multiplo; i++ )
      array[i] = new char[5];
    
    char* elemento = array[i];
    

In any case, I doubt you need a string matrix, otherwise you will need a char matrix.

On the other hand you are not calculating Multiplo correctly:

Num = strlen(Frase);
int Multiplo = Num/5;

What happens if the length is 12? Multipló will be worth 2 and then you will leave 2 unprocessed characters ...

You have to check that the rest is 0 and, if not, add one to Multiplo .

When you said

  

at the moment of inserting it does it well, since I enter and print everything from little to little.

You were cheating a bit, since you were only printing the character that you just stored, which, by logic, is going to be correct ... but with that you are not checking if you are crushing any previous character. Hence your initial confusion.

The corrected function:

int main()
{
    string Texto;
    string Texto2;
    char Frase[180];

    cout<<"Ingresa La Palabra A Encriptar: ";
    getline(cin,Texto);

    for(int  i = 0; Texto[i] != 0;++i){
        if(Texto[i] != 32)
            Texto2 = Texto2 + Texto[i];
    }
    cout<<"Sin Espacios: "<<Texto2<<endl;

    int Num = Texto2.length();
    strcpy(Frase, Texto2.c_str());
    int CaracteresRest = 5-(Num%5);
    if (CaracteresRest==5)
    {
        CaracteresRest=0;
    }
    Agregar(Frase,CaracteresRest);
    Num = strlen(Frase);
    int Multiplo = Num/5;
    if( Num % 5 != 0 ) Multiplo++;
    cout<<"Con *: "<<Frase<<endl;
    cout<<"**Arreglo**"<<endl;

    char *arreglo = new char[Multiplo*5];

    int ContTex = 0;
    for(int i = 0; i < Multiplo; i++){
        for(int j = 0; j <5; j++){
            arreglo[i*5+j] = Frase[ContTex];
            ContTex = ContTex + 1;
            cout<< arreglo[i*5+j] << ' ';
        }
        cout << '\n';
    }

    cout << '\n';
    for(int i = 0; i < Multiplo; i++){
        for(int j = 0; j <5; j++){
            cout << arreglo[i*5+j] << ' ';
        }
        cout << '\n';
    }

    delete[] arreglo;
}
    
answered by 02.11.2018 / 23:51
source