Error trying to access an array element

Asked

Viewed 302 times

0

How do I make the execution of mine loop generates a registration code automatically?

Error on line 36 - 'book[i]. Cod = i;' [Error] subscripted value is neither array nor Pointer nor vector

Objective since code: register 5 books with information of the same and generate a code automatically for each generated book.

Current code:

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

/*Definindo tamanho*/
#define TAM 5


int main(void) {


    /*--Criando a struct--*/
    struct ficha_do_livro {
        int cod;
        char nome_da_obra[50];
        char nome_do_autor[50];
        char nome_da_editora[50];
    }livro;


    /*--Cadastro do Livro--*/
    int i; 

    for (i = 0; i < TAM; i++) {

        printf("\n---------- Cadastro dos Livros -----------\n\n\n");

        livro[i].cod = i;
        printf("Insira o nome da Obra: ");
        fflush(stdin);
        fgets(livro.nome_da_obra, 40, stdin);

        printf("Insira o nome do autor: ");
        fflush(stdin);
        fgets(livro.nome_do_autor, 40, stdin);

        printf("Insira o nome da Editora: ");
        fflush(stdin);
        fgets(livro.nome_da_editora, 40, stdin);

    }
}

2 answers

3

Tam some errors in the code. The one that appears is that is trying to access an element of a array in something that is not a array. You declared a structure, and a variable with her type, but there only fits a single element. If you want to put several elements you need to put in the syntax you want this, indicating how many elements this variable will have. Then this error will disappear. But it will make other mistakes since the other members of struct after the code also need to be accessed by the index of the array, then said put the i there also.

Pay close attention because I have removed a lot of things that were not needed in this code, and packed up some things that make it more efficient. Yes, even the comment is not necessary because it says an obviousness, comments were not made for that. Don’t use things you don’t know what it’s for.

#include <stdio.h>

#define TAM 5

int main(void) {
    struct {
        int cod;
        char nome_da_obra[41];
        char nome_do_autor[41];
        char nome_da_editora[41];
    } livros[TAM];
    for (int i = 0; i < TAM; i++) {
        printf("\n---------- Cadastro dos Livros -----------\n");
        livro[i].cod = i;
        printf("Insira o nome da Obra: ");
        fgets(livro[i].nome_da_obra, 40, stdin);
        printf("Insira o nome do autor: ");
        fgets(livro[i].nome_do_autor, 40, stdin);
        printf("Insira o nome da Editora: ");
        fgets(livro[i].nome_da_editora, 40, stdin);
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • This is just the beginning of a project not the same complete, yet I will put more things so it is still in alpha. vlw by the attention

  • And what does that mean?

  • 1

    You wrote a very vague question, "And what does that mean?" does not specify the context of its comment referring to mine, specifically meant that it is not an objective project, focusing on scope and efficiency of it being its current structure focused on a test and learning development, the structuring of the comment in it is for the near future to have easy understanding of the scope of the algorithm itself.

  • Your comment is very vague, I gave an answer, you commented something that doesn’t seem to have any meaning. The context of my comment is your context-free comment. I haven’t said anything about the context of your project or what you want to do with it. I told you what mistake you made that has a lot of unnecessary things in it, and now that you give some context I can say that unnecessary things are bad for learning.

  • When you use what you don’t need, you learn wrong, and I hate when people learn wrong, so I always help in everything I can, not just by fixing the error presented, because it doesn’t teach. I usually say "working is different than being right". If you look here you’ll find this and an illustration of what most people’s software is like when they don’t care about doing it right. I noticed that you had the problem solved, but did not understand the solution, so I put in the reply description of the error and what I fixed, but it seems that you did not value it.

  • Not everyone knows how to structure all kinds of codes, if I’m here it’s because I’m about to clear my doubts and learn from them, I appreciate you helping, but understand that not every form of learning is given in the explanation of mistakes but also when we make them and correct them to learn from them. Learning not only gives a good semantic structure of the code the 'efficient' tornado, but also to err and recognize that you will never know enough about such a subject.

  • It’s your right to believe in this fallacy.

Show 2 more comments

2


You need to use an array of structures.
Obs. not tested.

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

/*Definindo tamanho*/
#define TAM 5

int main(void)
{

  /*--Criando a struct--*/
  struct ficha_do_livro
  {
    int cod;
    char nome_da_obra[50];
    char nome_do_autor[50];
    char nome_da_editora[50];
  } livro[TAM]; // <-----------------------

  /*--Cadastro do Livro--*/
  int i; 

  for (i = 0; i < TAM; i++)
  {
    printf("\n---------- Cadastro dos Livros -----------\n\n\n");

    livro[i].cod = i;
    printf("Insira o nome da Obra: ");
    fflush(stdin);
    fgets(livro[i].nome_da_obra, 40, stdin); // <---------------

    printf("Insira o nome do autor: ");
    fflush(stdin);
    fgets(livro[i].nome_do_autor, 40, stdin); // <-----------------------

    printf("Insira o nome da Editora: ");
    fflush(stdin);
    fgets(livro[i].nome_da_editora, 40, stdin); // <---------------------

  }
}
  • Vlw to all, you showed me where were the syntax errors, but I did not understand why of being stdin in fgets and not stdio...

Browser other questions tagged

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