Alejandra Garcia
Alejandra Garcia

Reputation: 119

Unable to get the function working, program always crashes after prompt

Since in C its seems to be impossible to use switch with chars(only integers) im trying to make a simple prompt using the if function, the problem is after scanf point the program always crashes at this points puts("Confirmas que quieres borrar[s\\n]") or "are you sure you wanna erase it?".

void borrar_peliculas(struct videoclub peliculas[30],int p) {
    char titulo_pelicula[30], resp_eliminar;
    int k = 0;

    puts("\a\nBorrador de peliculas");

    puts("\nDime el titulo de la pelicula a ser borrada\n");
    scanf("%30s", titulo_pelicula);

    for (k = 0;k < p ;k++) {

        if (strcmp(peliculas[k].nombre,titulo_pelicula) ==0 ) {

            puts("\nConfirmas que quieres borrar[s\\n]\n");
            fflush(stdin);
            scanf("%c", resp_eliminar);

            if(resp_eliminar=='s') {

                peliculas[k].id = 0;
                strcpy(peliculas[k].nombre," ");
                strcpy(peliculas[k].categoria," ");
                strcpy(peliculas[k].nom_actor," ");
                strcpy(peliculas[k].ape_actor," ");
                peliculas[k].nota = 0;

                break;

            }

            if(resp_eliminar=='n') {
                puts("\a\nSaliendo\n");
                break;
            }

        } else {
            puts("\nNo encontrado\n");
        }
    }
}

Upvotes: 0

Views: 77

Answers (2)

David Williams
David Williams

Reputation: 21

scanf takes a pointer. If you replace scanf("%c", resp_eliminar); with scanf("%c", &resp_eliminar); it will work.

Upvotes: 2

PaulMcKenzie
PaulMcKenzie

Reputation: 35455

You have one very big error here. The scanf function requires a pointer to the variable that will store the input. Your current program invokes undefined behavior.

Instead of this:

scanf("%c", resp_eliminar);

Do this:

scanf("%c", &resp_eliminar);

Upvotes: 1

Related Questions