Problem with the execution of the program

0

What I want to do, is for the user to enter a text, then in the formaTabla function that compares the string entered with the ones stored in the table and if the string is not repeated, save it in the table .

The problem I have is that when I execute it, it executes everything until it shows the string entered, then it does not work anymore. I would like to know if I have an error that I'm not really finding.

And I would like to know how is the syntax to reserve memory for an arrangement of structures, since I try it but it marks me error

struct tab
{ char cadena[25];
int alfa; };


char * ingresaCadena ();
struct tab* formaTabla (char *, int *filas);

int main()
{
char *texto,rta='s';
int filas=0,op;
struct tab *puntabla;

printf("Ingrese una de las siguientes opciones:  \n");
printf(" 1-Ingresar texto y formar tabla \n 2-Buscar \n 3-Salir \n");
scanf("%d",&op);
while(getchar()!='\n');
while (op>0 && op<4)
     { switch (op)
             {  case 1:  while(rta=='s')
                           { texto=ingresaCadena();
                             printf("El texto ingresado es:   %s  \n",   texto);
                             puntabla=formaTabla(texto,&filas);
                             printf ("Desea ingresar otro texto? S o N \n");
                             scanf("%c",&op); }
                        for (int i=0; i<filas;i++)
                                { printf("Las cadenas ingresadas son: \n");
                                   printf("%s \n", *puntabla->cadena); } } }













system("PAUSE");
return 0;
}

char * ingresaCadena ()
{
 char *t=(char*)calloc(25,sizeof(char));
 printf("Ingrese el texto \n");
 gets(t);
 return t; }

 struct tab* formaTabla (char *tex, int *f)
 {
 struct tab tabla[100];
 int var,longi;
 for (int i=0;i<=*f;i++)
 {var=strcmp(tabla[i].cadena,tex);
    if (var!=0)
      { strcpy(tabla[*f].cadena,tex);
        longi=strlen(tabla[*f].cadena);
        tabla[*f].alfa=longi;
        *f++; } }
  return (&tabla[*f-1]) ; }
    
asked by Maca Igoillo 03.02.2017 в 13:59
source

1 answer

3

If a program is frozen it is usually a blockage caused by a loop.

In your program you only have two loops, then the problem must find there ... effectively:

  while(rta=='s')
  { texto=ingresaCadena();
    printf("El texto ingresado es:   %s  \n",   texto);
    puntabla=formaTabla(texto,&filas);
    printf ("Desea ingresar otro texto? S o N \n");
    scanf("%c",&op);
  }

The loop verifies the value of rta ... but that variable is not modified inside the loop, then there will be no way out ... it is a problem associated with the copypaste .

The solution is simple ... replace &op with &rta .

Of course, consider taking out the two bulks of case .

EDITO

Even if you are not reporting it, since you have not arrived yet, you have other problems in the code:

struct tab* formaTabla (char *tex, int *f)
{
  struct tab tabla[100];
  // ...
  return (&tabla[*f-1]);
}

Since tabla is not a static variable, the pointer you return will not be valid and will give you problems. It does not make much sense that the table is internal to the function ... common sense tells me that structure would make more sense than was declared outside the function:

int main()
{
  struct tab tabla[100];
  // ...
  while (op>0 && op<4)
  {
    switch (op)
    {
    case 1:
      while(rta=='s')
      {
        // ...
        formaTabla(tabla, texto,&filas);
      }
    }
  }
}

void formaTabla(sturct tab* tabla, char* tex, int* f)
{
  // struct tab tabla[100] <<--- Ya no es necesario
}

Another error in your code, although less visible, is that it has memory leaks. Your dynamic memory reservation function:

char *t=(char*)calloc(25,sizeof(char));

Where is the memory released? Nowhere. I would suggest you do not make memory reservations in your program given how simple it is .. but if you do at least worry about releasing the memory:

texto=ingresaCadena();
printf("El texto ingresado es:   %s  \n",   texto);
puntabla=formaTabla(texto,&filas);
free(texto); // <<---
    
answered by 03.02.2017 / 14:12
source