I’m having trouble reading a string inside a function

Asked

Viewed 99 times

0

Good evening, I would like to know methods to read a string in C(Version C99), I have tried a lot, and does not work, below, the defective code.

Obs: The error that reports is that just after pressing "ENTER" on the first 'gets' of the registration function, the program Buga, and displays the message "the program.exe has stopped working".

Obs.1: I removed the < > includes as the browser identifies this as an html tag and does not display the contents of includes.

#include stdio.h
#include stdlib.h

int menu()
{
    int opc;
    printf("\n Opcoes: \n1. Cadastrar livros\n2. Consultar livros\n3. Alterar informacoes de livros\n4. Remover um livro da lista");
    scanf("%d", &opc);
    return(opc);
}
struct informacoes
{
    char nome[100];
    char autor[100];
    long long int isbn;
    char volume[10];
    int data;
    char editora[100];
    int paginas;
};
int cadastro(struct informacoes *livros, int i){
    printf("\nNome do livro: ");
    gets(livros[i].nome);
    printf("\nAutor: ");
    gets(livros[i].autor);
    printf("\n ISBN : ");
    scanf("%lli", livros[i].isbn);
    printf("\n Volume em romanos: ");
    gets(livros[i].volume);
    printf("\n Ano de lancamento: ");
    scanf("%d", livros[i].data);
    printf("\nEditora do livro: ");
    gets(livros[i].editora);
    printf("\nQuantidade de paginas no livro: ");
    scanf("%d", livros[i].paginas);
}
int main() 
{
    int opc = menu();
    struct informacoes livros[10];
    int i=0;
    switch(opc)
    {
        case 1:
            cadastro(livros, &i);
    }
    return (EXIT_SUCCESS);
}
  • when I messed with C, he’s about 4 years old, or he used gets or scanf, if he tried to use both, stick in the right kkkk

  • I guess that’s not the problem, kkk

  • Try running the program with a Debugger and see the error that returns.

  • Appears 1 Warning and 1 note, attachment:

  • dnvisso. c:44:30: Warning: Passing argument 2 of 'register' makes integer from Pointer without a cast [-Wint-Conversion] register(books, &opc);

  • dnvisso. c:19:5: note: expected 'int' but argument is of type 'int *' int cadastro(struct informacoes *livros, int i){

  • The problem is in its parameters, ie the (struct informacoes *livros, int i), I’m trying to figure out why.

Show 2 more comments

2 answers

1


Your code actually has several errors, the 2 clearest ones are:

  1. You cannot send a pointer to a method that you ask for an integer value as a parameter:

Where did you put:

cadastro(livros, &i);

Exchange for:

cadastro(livros, i); //Note que não possui o &
  1. The method cadastro is the type int but returns no value, put it of type void, that is, nothing returns:

Where did you put:

int cadastro(struct informacoes *livros, int i){

Exchange for:

void cadastro(struct informacoes *livros, int i){
  • It worked! Thanks again, and sorry for the mistakes being so simple

  • 1

    From what you’re saying, it seems to be the case mark an answer as accepted. If you have an answer that really helped you, mark it as accepted. So content is more organized and easier to find in the future by other people with similar problems.

0

do so:

scanf("%[ n]s",variablename);

  • The problem persists, has as you see for me if the problem is in the syntax?

Browser other questions tagged

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