Question :
Good evening, I have a job of completing discipline in which I have to do registration of N employees, in my code I put 5 to get the tests faster.
This information would be saved in a structure and how I would use the structure more than once, both for registration and for updating the register I decided to use the same code together with switch case. But then I thought, since this structure will be used more than once, I can create a function with it and call when necessary, but it is not working, can someone tell me where I am wrong? Here is the code:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
struct funcionarios
{
int cod_func;
int cod_cargo;
char end_func[100];
char nome_func[100];
};
struct funcionarios x[4];
struct funcionarios cadastro1(struct funcionarios c)
{
for(c = 0; c < 5; c++)
{
setbuf(stdin, NULL);
printf("Digite o nome completo do funcionário: ");
gets(x[c].nome_func);
setbuf(stdin, NULL);
printf("Digite o endereço do funcionário: ");
gets(x[c].end_func);
setbuf(stdin, NULL);
printf("Digite o código do funcionário: ");
scanf("%i", &x[c].cod_func);
setbuf(stdin, NULL);
printf("Digite o código do cargo: ");
scanf("%i", &x[c].cod_cargo);
printf("n");
}
return c;
};
int main()
{
setlocale(LC_ALL, "");
int i, c, f, controle1, controle2, controle3, controle4, controle5;
int menu=0, menu1=0, menu2=0, menu3=0, menu4=0, menu5=0, menu6=0;
float cargos[5] = {2.500, 1.500, 12.000, 1.800, 950};
do
{
if(menu < 0 || menu > 6)
{
printf("nEntrada inválida, Selecione conforme mostrado.n");
printf("Opções válidas de 1 até 6 e 7 para encerrar.n");
}
printf("nnSelecione uma opção conforme mostrado abaixo.nn");
printf("1 - Cargos da Empresa.n");
printf("2 - Funcionários da Empresa.n");
printf("3 - Exibir relatório completo.n");
printf("4 - Exibir média salarial dos funcionários.n");
printf("5 - Exibir o valor pago que pertençam a determinado cargo.n");
printf("6 - Ordenação.n");
printf("7 - Sair.nn");
printf("Digite a opção desejada: ");
scanf("%i", &menu);
controle1 = menu;
system("cls");
}
while (menu < 0 || menu > 6);
switch (controle1)
{
case 1:
{
do
{
system("cls");
printf("nSelecione uma opção.nn");
printf("1 - Cadastrar Cargos: n");
printf("2 - Atualizar Cargos existentes: n");
printf("nDigite a opção desejada: ");
scanf("%i", &menu1);
controle2 = menu1;
}
while(menu1 < 1 || menu1 > 2);
}
switch (controle2)
{
case 1:
{
system("cls");
printf("CADASTRO DE CARGOS, PREENCHA FORMULÁRIO:nn");
cadastro1(c); //Aqui chamo a função para preenche o formulário
break;
}
case 2:
{
system("cls");
printf("ALTERAR INFORMAÇÕES DE CARGOS, PREENCHA:nn");
cadastro1(c); ////Aqui chamo a função para preenche o formulário
break;
}
}
}
}
I made a version that works from the code but using switch, that at least executes … But the function would be to optimize the code.
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
struct funcionarios
{
int cod_func;
int cod_cargo;
char end_func[100];
char nome_func[100];
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
setlocale(LC_ALL, "");
int i, c, f, controle1, controle2, controle3, controle4, controle5;
int menu=0, menu1=0, menu2=0, menu3=0, menu4=0, menu5=0, menu6=0;
float cargos[5] = {2.500, 1.500, 12.000, 1.800, 950};
struct funcionarios x[4];
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
do
{
if(menu < 0 || menu > 6)
{
printf("nEntrada inválida, Selecione conforme mostrado.n");
printf("Opções válidas de 1 até 6 e 7 para encerrar.n");
}
printf("nnSelecione uma opção conforme mostrado abaixo.nn");
printf("1 - Cargos da Empresa.n");
printf("2 - Funcionários da Empresa.n");
printf("3 - Exibir relatório completo.n");
printf("4 - Exibir média salarial dos funcionários.n");
printf("5 - Exibir o valor pago que pertençam a determinado cargo.n");
printf("6 - Ordenação.n");
printf("7 - Sair.nn");
printf("Digite a opção desejada: ");
scanf("%i", &menu);
controle1 = menu;
system("cls");
}
while (menu < 0 || menu > 6);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
switch (controle1)
{
case 1:
{
do
{
system("cls");
printf("nSelecione uma opção.nn");
printf("1 - Cadastrar Cargos: n");
printf("2 - Atualizar Cargos existentes: n");
printf("nDigite a opção desejada: ");
scanf("%i", &menu1);
controle2 = menu1;
}
while(menu1 < 1 || menu1 > 2);
}
switch (controle2)
{
case 1:
{
do
{
system("cls");
printf("CADASTRO DE CARGOS, PREENCHA FORMULÁRIO:nn");
for(c = 0; c < 5; c++)
{
setbuf(stdin, NULL);
printf("Digite o nome completo do funcionário: ");
gets(x[c].nome_func);
setbuf(stdin, NULL);
printf("Digite o endereço do funcionário: ");
gets(x[c].end_func);
setbuf(stdin, NULL);
printf("Digite o código do funcionário: ");
scanf("%i", &x[c].cod_func);
setbuf(stdin, NULL);
printf("Digite o código do cargo: ");
scanf("%i", &x[c].cod_cargo);
printf("n");
}
//printf("Retornar para tela principal? SIM - 1 / NÃO - 2: ");
//scanf("%i", &controle1);
}
while(x[i].cod_cargo < 1 || x[i].cod_cargo > 5);
}
case 2:
{
do
{
system("cls");
printf("ALTERAR INFORMAÇÕES DE CARGOS, PREENCHA:nn");
for(c = 0; c < 5; c++)
{
setbuf(stdin, NULL);
printf("Digite o nome completo do funcionário: ");
gets(x[c].nome_func);
setbuf(stdin, NULL);
printf("Digite o endereço do funcionário: ");
gets(x[c].end_func);
setbuf(stdin, NULL);
printf("Digite o código do funcionário: ");
scanf("%i", &x[c].cod_func);
setbuf(stdin, NULL);
printf("Digite o código do cargo: ");
scanf("%i", &x[c].cod_cargo);
printf("n");
}
//printf("Retornar para tela principal? SIM - 1 / NÃO - 2: ");
//scanf("%i", &controle1);
}
while(x[i].cod_cargo < 1 || x[i].cod_cargo > 5);
}
}
}
}
Can anyone give me a light on how I can pass the structure to function and call it whenever the user is using it? To register or replace information …
Answer :
You can do as @ JonathanBarbosa19 explained by returning the struct created. But you can also go through the reference parameter.
typedef struct funcionarios
{
int cod_func;
int cod_cargo;
char end_func[100];
char nome_func[100];
}Funcionarios;
//criei um apelido para a tua struct para não colocar struct toda vez
Funcionarios x[4];//não deves usar variaveis globais. Remove isto e //declara no main
//Não precisa retornar pois recebe o parâmetro por referência e já altera //o mesmo.
void cadastro(Funcionarios *c) {
//quando ponteiro, acessa-se a struct com -> ao inves do .
setbuf(stdin, NULL);
printf("Digite o nome completo do funcionário: ");
gets(c->nome_func);
setbuf(stdin, NULL);
printf("Digite o endereço do funcionário: ");
gets(c->end_func);
setbuf(stdin, NULL);
printf("Digite o código do funcionário: ");
scanf("%i", &c->cod_func);
setbuf(stdin, NULL);
printf("Digite o código do cargo: ");
scanf("%i", &c->cod_cargo);
printf("n");
}
}//tinha um ; errado aqui
int main() {
Funcionarios x[3];
for(int i=0; i<3; i++){
cadastro(x[i]);//como se trata de um vetor, não se coloca o &
}
return 0;
}
From what I understand about your code, your function is trying to use a officials value as if it were an Int within for . This is why the function has an error.
The best way to perform the desired function is:
Struct funcionarios cadastro1(){
struct funcionarios func;
printf("Digite o nome completo do funcionário: ");
gets(func.nome_func);
setbuf(stdin, NULL);
printf("Digite o endereço do funcionário: ");
gets(func.end_func);
setbuf(stdin, NULL);
printf("Digite o código do funcionário: ");
scanf("%i", &func.cod_func);
setbuf(stdin, NULL);
printf("Digite o código do cargo: ");
scanf("%i", &func.cod_cargo);
printf("n");
return (func);
}
In this way your function will always create a new employee and return it.
To put you on your employee list you can apply it directly to Main , for example:
struct funcionarios x[3];
for(int i=0; i<3; i++){
x[i]= cadastro1();
}
In this case you will have to create another function that receives a officials parameter to edit its attributes. The better you encapsulate, the easier it will be to use within the algorithm.
Any questions, please let us know!