0
I am creating a user register using struct
. In it, there’s the CPF field. As it is a very large field to store in number format, I decided to save as a string, as well as in a database where Cpf is saved with varchar
.
I do a validation to not allow registering 2 equal numbers. For this, first I save the value in an auxiliary variable and passing the validation I saved in the vector. The problem is when I pass the value of the variable to the vector.
Error on the line 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 put 10-12-15 digits. I tried using the type double
, but when the first character is zero-0, it does not store.
How can I fix this? Where am I going wrong?
I didn’t know the
string.h
, very interesting! I will read about it to learn even more. The code spun! Thank you very much Lacobus.– GtGtGt