Error while running C program

Asked

Viewed 75 times

0

I have to create a movie registration program, but when I run the program and it’s wrong and I don’t know why it doesn’t run, can help me?

#include <stdio.h>
#include <locale.h>
#include <string.h>
#include <stdlib.h>

int main()

{
    struct informacao_sobre_os_filme
    {
       int ano_de_lancamento, faixa_etaria; 
       double duracao;
       char nacionalidade[200], nome_do_filme[200], genero[200];

    };

    struct informacao_sobre_os_filme;

    printf("CADASTRMENTO DE FILMES: \n");
    printf("Nome do filme: \n");
    scanf("%c", &nome_do_filme);
    return 0;
}

1 answer

0

There are several problems in the code presented.

  • %c is to read a caretere. To read a string you must use %s
  • The declaration of a variable of the structure type struct informacao_sobre_os_filme; lost the variable name.
  • The structure must be declared before the main how is convention and so can used by various functions.

Fixing all this would be like this:

struct informacao_sobre_os_filme {
    int ano_de_lancamento, faixa_etaria;
    double duracao;
    char nacionalidade[200], nome_do_filme[200], genero[200];
};

int main(){
    struct informacao_sobre_os_filme filme1; //agora variavel chamada filme1

    printf("CADASTRMENTO DE FILMES: \n");
    printf("Nome do filme: \n");
    scanf("%s", filme1.nome_do_filme); //ler para o filme1 no campo nome_do_filme
    return 0;
}

Notice that like the nome_do_filme is an array of characters, is already technically a pointer to the first, so it does not take the & in the scanf.

If you want you can even use a more robust form of reading that would be with the fgets. With fgets can read more than one word and ensure that it does not read more than the space it allocated, which would be in this case are 200 characters:

...
printf("Nome do filme: \n");
fgets(filme1.nome_do_filme, 200, stdin);
...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.