Problem with Struct and Function in C

1

Good evening, I have a problem with the code I'm doing. I'm not sure if I'm deleting the values correctly (and replacing the next). The error is appearing in the substitution by the next and I do not know if I'm correctly passing the x, which is the index.

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;
}
    
asked by anonymous 30.09.2016 / 22:52

2 answers

1

There are (at least) 2 errors.

The first is to use "% c" when it should be "% s".

The second is the declaration of the structure:

struct FichaCarro
{
   char fabricante[15];
   char modelo[15];
   char combustivel[10];
   char cor[10];
   char placa[10];
   int ano[10];
   int km[15];
   float preco; // era "float preco[15]"
};
    
01.10.2016 / 01:16
4

In the declaration of the struct shown, the data ano , km and preco are vectors and not just a data as I think it should be. To simplify the declaration would look like this:

struct fichacarro 
{

    char   fabricante[15]; 
    char   modelo[15]; 
    char   combustivel[10]; 
    char   cor[10];
    char   placa[10];
    int    ano;
    int    km;
    float  preco;

};

To read the strings in the inserir function, opt to use the function fgets ( Suggested reading ).

printf("Modelo: ");
fgets(carro[x].modelo, 15, stdin);
fflush(stdin);
printf("\nFabricante: ");
fgets(carro[x].fabricante, 15, stdin);
fflush(stdin);
printf("\nCombustivel: ");
fgets(carro[x].combustivel, 10, stdin);
fflush(stdin);
printf("\nCor: ");
fgets(carro[x].cor, 10, stdin);
fflush(stdin);
printf("\nPlaca: ");
fgets(carro[x].placa, 10, stdin);

A tip at the end of the code at test time, the index must be the value entered x to confirm that everything is actually working

printf("%.2f\n", carro[x].preco);
    
01.10.2016 / 01:45