Build error in C++ when accessing structure in function

Asked

Viewed 226 times

1

I’m creating a code that holds the information of three people inside one struct, but I am having a build error when I will save the age option on struct. The mistake is this:

/home/arthur/Área de Trabalho/pj/pj.cpp: In function ‘void 
cadastrar()’:
/home/arthur/Área de Trabalho/pj/pj.cpp:42:17: error: ‘pessoas’ was 
not declared in this scope
 //  scanf("%d",&pessoas[i].idade);

             ^~~~~~~

And the code is this:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#DEFINE MAX_PESSOAS[3]
typedef struct{
  char nome[30];
  char profissao[30];
  int  idade;
  int ativado;
}comp;

void cadastrar();

int main(int argc, char const *argv[]) {
  comp pessoas[3];

  int op;
  do{
    printf("\nPara adastrar pessoas aperte 1\n");
    scanf("%d", &op );
    getchar();
    switch (op) {
  case 1:
    cadastrar();
  break;
}
}while(op!=0);
}
void cadastrar(){
  char nome[30];
  char profissao[30];
  int idade;
  int op;
do{
  printf("\nNome: ");
  getchar();
  fgets(nome,sizeof(nome),stdin);
  printf("\nProfissão: ");
  fgets(profissao,sizeof(profissao),stdin);
  printf("\nIdade: ");
  scanf("%d", &idade);
for (size_t i = 0; i < 3; i++) {
  //if (pessoas[i].ativado==0) {
    strcpy(pessoas[i].nome, nome);
    strcpy(pessoas[i].profissao, profissao);
    pessoas[i].idade = idade;
    break;
//  }
}
  printf("\n1 - Continuar\n0 - para sair\n");
  printf("Digite sua escolha: ");
  scanf("%d", &op );
}while (op!=0);

}
  • the error message is clear: the variable "people" is not declared at the place ("scope") where it was used...the solution is to declare this variable

  • That’s what I don’t understand, she’s no longer declared under the main?

  • I managed to fix this error by modifying the statement means of struct. struct cadastro{ char nome[30]; char profissao[30]; int age; int enabled; }; void cadastrar(); struct cadastro pessoas[3];

  • @aguiarito Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already done so. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

2 answers

1


The code has several problems, and gives an indication that it does not understand what it is doing. Learning to program should be step by step, learn to lay each brick to build the whole house, want to make the house without understanding every detail does not work and will be run over all the time. And it gets more complicated and harder to solve in the future. I improved several things in the code, but still there are many others that could be improved.

I didn’t see the point in that for and there is a lot of use of unnecessary things, but it lacks a fundamental, variable that stores the data in the function cadastrar(). How did you create it in main() and for an exercise it makes sense you have to pass it as an argument to the function that makes the registration, and of course, the function should receive this as a parameter.

Reinforcement that did not solve everything, just improved and solved the problem described.

#include <stdio.h>

typedef struct {
    char nome[31];
    char profissao[31];
    int  idade;
    int ativado;
} Pessoa;

void cadastrar(Pessoa pessoas[3]) {
    int op;
    int i = 0;
    do {
        printf("\nNome: ");
        fgets(pessoas[i].nome, sizeof(pessoas[i].nome), stdin);
        printf("\nProfissão: ");
        fgets(pessoas[i].profissao, sizeof(pessoas[i].profissao), stdin);
        printf("\nIdade: ");
        scanf("%d", &pessoas[i].idade);
        printf("\n1 - Continuar\n0 - para sair\n");
        printf("Digite sua escolha: ");
        scanf("%d", &op);
        i++;
    } while (i < 3 && op != 0);
}

int main() {
    Pessoa pessoas[3];
    int op;
    do {
        printf("\nPara cadastrar pessoas aperte 1\n");
        scanf("%d", &op);
        switch (op) {
        case 1:
            cadastrar(pessoas);
            break;
        }
    } while (op != 0);
}

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

0

Best a purely C solution++:

#include <iostream>
#include <string>
#include <vector>

struct Pessoa {
    std::string nome;
    std::string profissao;
    int idade;
    bool ativado;
};

void cadastrar(std::vector<Pessoa>& pessoas)
{
    int op = 0;
    do {
        Pessoa p;
        std::cout << "\nPessoa: ";
        std::cin >> p.nome;

        std::cout << "\nProfissão: ";
        std::cin >> p.profissao;

        std::cout << "\nIdade: ";
        std::cin >> p.idade;

        pessoas.push_back(p);

        std::cout << "\n1 - continuar\n0 - sair\n";
        std::cin >> op;
    } while (op == 1);
}

int main() {
    std::vector<Pessoa> pessoas;

    int op;
    do {
        std::cout << "\nPara cadastrar pessoas aperte 1\n 0 - sair\n";
        std::cin >> op;
        if (op == 1) {
            cadastrar(pessoas);
            break;
        }
    } while (op != 0);
}

Browser other questions tagged

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