-2
In the code below I am trying to define a structure whose members are pointers that will be used to work with the name, email and age data, all dynamically allocated, of an employee. However, the programme closes when qtdeFuncionarios > 1
.
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
struct funcionario{
char *nome;
char *email;
int *idade;
};
int main() {
int opcao, qtdeFuncionarios, i;
printf("1.Cadastrar funcionario\n");
printf("2.Listar funcionarios\n");
printf("3.Editar dados de funcionario\n");
printf("4.Excluir funcionario\n");
scanf("%d", &opcao);
if(opcao = 1){
printf("Quantos funcionarios deseja cadastrar? ");
scanf("%d", &qtdeFuncionarios);
struct funcionario *f = (struct funcionario *)malloc(qtdeFuncionarios * sizeof(struct funcionario));
for(i = 0; i < qtdeFuncionarios; i++){
char *nome = (char *)malloc(35*sizeof(char));
printf("Digite o nome do funcionario %d: ", i);
scanf("%s", nome );
strcpy(f[i].nome, nome);
printf("%s", f[i].nome );
char *email= (char *)malloc(35*sizeof(char));
printf("Digite o email do funcionario %d: ", i);
scanf("%s", email);
strcpy(f[i].email, email);
printf("%s", f[i].email );
printf("Digite a idade do funcionario %d: ", i);
scanf("%d", &f[i].idade);
printf("%d", f[i].idade );
}
}
return 0;
}
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. 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