1
Good evening, I’m having a problem with the code I’m doing. I don’t know if I’m deleting the values correctly (and replacing them with the next one). The error is appearing in the replacement by the next one and I don’t know if I am correctly passing the x, which is the Indice.
struct fichacarro {
char fabricante[15];
char modelo[15];
char combustivel[10];
char cor[10];
char placa[10];
int ano;
int km;
float preco;
};
int inserir(struct fichacarro carro[], int *x){
printf("\nModelo: ");
fflush(stdin);
fgets(carro[*x].modelo, 15, stdin);
printf("Fabricante: ");
fflush(stdin);
fgets(carro[*x].fabricante, 15, stdin);
printf("Combustivel (alcool, gasolina ou diesel): ");
fflush(stdin);
fgets(carro[*x].combustivel, 10, stdin);
printf("Cor (branco, preto ou prata): ");
fflush(stdin);
fgets(carro[*x].cor, 10, stdin);
printf("Placa: ");
fflush(stdin);
fgets(carro[*x].placa, 10, stdin);
printf("Ano: ");
scanf("%d", &carro[*x].ano);
printf("Kilometros: ");
scanf("%d", &carro[*x].km);
printf("Preco: ");
scanf("%f", &carro[*x].preco);
*x=*x+1;
system("cls");
}
int excluir(struct fichacarro carro[], int *x){
int k, j;
printf("Digite o indice do carro que quer excluir: ");
scanf("%d", &k);
k=k-1;
if(k>*x || k<0)
printf("Indice nao existe");
else {
j=k;
while (j<*x){
carro[j].modelo=carro[j+1].modelo;
carro[j].fabricante=carro[j+1].fabricante;
carro[j].combustivel=carro[j+1].combustivel;
carro[j].cor=carro[j+1].cor;
carro[j].placa=carro[j+1].placa;
carro[j].ano=carro[j+1].ano;
carro[j].km=carro[j+1].km;
carro[j].preco=carro[j+1].preco;
j=j+1;
}
*x=*x-1;
}
}
int pesquisar(){
}
int main(){
struct fichacarro carro[49];
int n, x=0, y, cont;
printf("Numero de veiculos: ");
scanf("%d", &n);
system("cls");
for (cont=0;cont<n;cont++){
printf("----OPCOES DE ESCOLHA----\n\n");
printf("1 - Inserir veiculo\n");
printf("2 - Excluir veiculo\n");
printf("3 - Pesquisar veiculo\n");
printf("4 - Sair\n\n");
printf("Opcao escolhida: ");
scanf("%d", &y);
switch (y){
case 1:
inserir(carro, &x);
break;
case 2:
excluir(carro, &x);
break;
case 3:
pesquisar();
break;
case 4:
x=n;
break;
}
}
return 0;
}
Use
scanf("%c", &carro[x].placa);
will read only a single character. I think you want to read a string, iefgets(carro[x].placa, 10, stdin);
– Victor Stafusa
or use gets(car[x].plate); ? but the problem happened in the real number, there is something out of the pattern in the struct?
– Ygor Fraga
Do not use the function
gets
never! She is hated and considered obsolete for a good reason: buffer overflow. And in your case, it is very easy to cause such a problem, just type more than 10 characters for it to go out corrupting your program’s memory and causing possible Segmentation faults. Use thefgets(carro[x].placa, 10, stdin);
to already do right from the start and avoid future headaches.– Victor Stafusa
I see that if you don’t start inserting the data when the
x
for zero, there would be no other opportunity to do so before the program ends, since it doesprintf("%.2f", carro[0].preco);
instead ofprintf("%.2f", carro[x].preco);
.– Victor Stafusa
Then replace 0 by x and put fgets? has no error in passing the vector to the function?
– Ygor Fraga
I don’t know if there are any more mistakes. But replacing 0 by x and placing fgets should help.
– Victor Stafusa