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
– zentrunix
That’s what I don’t understand, she’s no longer declared under the main?
– aguiarito
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
@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).
– Maniero