I am creating a user registry using struct
. In it, there is the CPF field. As it is a very large field to save in numbers format, I decided to save as string, as well as in database where cpf is saved with varchar
.
I do a validation not to allow 2 cpfs to be registered. For this, first I save the value in an auxiliary variable and pass in the validation I saved in the vector. The problem is when I pass the value of the variable to the vector.
Line error where I pass the value of the variable to the vector: vet[i].cpf = cpfAux;
[Error] assignment to expression with array type
My code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct colaborador{
char nome[50];
char cpf[18];
char nascimento[11];
float salario;
};
struct colaborador vet[2];
int i, j;
char nomePesquisa[30];
char cpfAux[18];
int contPesquisa = 0;
bool rep = false;
int cadastra(){
for(i=0; i<2; i++){
printf("Insira o nome:");
gets(vet[i].nome);
printf("Insira o cpf:");
gets(cpfAux);
for(j=0; j<2; j++){
while(strcmp(cpfAux,vet[j].cpf) == 0) {
rep = true;
printf("ERRO, CPFS IGUAIS!\n\n");
printf("Insira o cpf novamente:");
gets(cpfAux);
}
rep = false;
}
if(rep == false){
vet[i].cpf = cpfAux;
}
printf("Insira o nascimento:");
gets(vet[i].nascimento);
printf("Insira o salario:");
scanf("%f", &vet[i].salario);
fflush(stdin);
}
}
int pesquisa(){
printf("Insira um colaborador a ser pesquisado:");
scanf("%s", &nomePesquisa);
for(i=0; i<2; i++){
if(strcmp(nomePesquisa,vet[i].nome) == 0){
printf("Nome: %s\n", vet[i].nome);
printf("CPF: %s\n", vet[i].cpf);
printf("Nascimento: %s\n", vet[i].nascimento);
printf("Salario: %2.f\n", vet[i].salario);
exit(0);
} else {
contPesquisa++;
}
}
if(contPesquisa > 0){
printf("Colaborador nao encontrado.");
}
}
int main(int argc, char *argv[]) {
cadastra();
pesquisa();
return 0;
}
I tested the code using int
for cpf and it worked. The problem is when I enter 10-12-15 digits. I tried to use type double
, but when the first character is zero-0, it does not store.
How can I fix this? Where am I going wrong?