Problem with Struct and Function in C

Asked

Viewed 279 times

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, ie fgets(carro[x].placa, 10, stdin);

  • or use gets(car[x].plate); ? but the problem happened in the real number, there is something out of the pattern in the struct?

  • 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 the fgets(carro[x].placa, 10, stdin); to already do right from the start and avoid future headaches.

  • 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 does printf("%.2f", carro[0].preco); instead of printf("%.2f", carro[x].preco);.

  • Then replace 0 by x and put fgets? has no error in passing the vector to the function?

  • I don’t know if there are any more mistakes. But replacing 0 by x and placing fgets should help.

Show 1 more comment

2 answers

4

In the struct declaration provided, the data ano, km and preco are vectors and not just a given as I think it should be. To simplify the statement 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 strings in the function inserir for using 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 the time of the test, the Indice must be the entered value x to confirm that everything is really working

printf("%.2f\n", carro[x].preco);
  • Thanks a lot man, it worked, I was still having doubts about struct and string, but now I could understand well.

  • It’s always good to help. @Ygorfraga If my answer has helped you find the solution, check to accept reply.

  • Okay, I did that, can I ask you another question? You are giving an error in data deletion. I will edit and put the current code, you can see the removal part?

  • To assign one string to another do not only string1 = string2. Instead use the function strcpy() library string.h. As you are picking which car is being deleted within the function, the variable x does not need to be passed as parameter. Take an example: https://ideone.com/N7x85C

  • Okay, I’ll use it, but on the whole and the real part is right? I used right the x and the pointer question?

  • The whole and the real is yes. The x apparently correct as well. If it’s not working, ask another question showing the function excluir , and how you’re using it.

  • All right, it all worked out, I got a problem with the search function, can you check it out? http://answall.com/questions/156130/problema-com-pesquisa-com-struct

Show 2 more comments

1


There are (at least) 2 errors.

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

The second is the statement 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]"
};
  • I thought I could use all the variables in the same char and I thought it could be the c and not the s in the reading. Thanks man, helped me a lot, it worked here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.