Problem with Struct Research

Asked

Viewed 65 times

0

In my code, I can enter and delete normally, but in the searches it does not print, what could be the problem? I thought it would work the way I did. I’m doing the 'if' correctly?

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, int *f){





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;
*f=*f+1;
system("cls");

}
int excluir(struct fichacarro carro[], int *x, int *f){
    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){
                strcpy(carro[j].modelo, carro[j+1].modelo);
                strcpy(carro[j].fabricante, carro[j+1].fabricante);
                strcpy(carro[j].combustivel, carro[j+1].combustivel);
                strcpy(carro[j].cor, carro[j+1].cor);
                strcpy(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;
                *f=*f-1;
            }
    }

int pesquisar(struct fichacarro carro[], int *x, int f){
    int y, j;
    char cor[10];
    float preco;

    char fabricante[15];
    printf("\n----OPCOES DE PESQUISA----\n");
    printf("1 - Ler o nome do fabricante e informar todos os veiculos deste         fabricante.\n");
    printf("2 - Pesquisar os dados de veículos com o preço dentro de um limite fornecido.\n");
    printf("3 - Informar todos os veículos de determinada cor pesquisada\n\n");
    printf("Opcao escolhida: ");
    scanf("%d", &y);

        switch (y){

            case 1:
                printf("\nDigite o nome do fabricante: ");
                fflush(stdin);
                fgets(fabricante, 15, stdin);
                j=*x;

                while (j<f){
                    if(strcmp (fabricante, carro[j].fabricante) == 0 );{
                        printf("\nCarro: %d", j);
                        printf("\nModelo: %s\n", carro[j].modelo);
                    }
                    j=j+1;
                }
                system("pause");
                system("cls");
                break;

            case 2:
            printf("Preco limite: ");
            scanf("%f", &preco);
            j=*x;
             while (j<f){
                if (carro[j].preco<=preco);{
                    printf("\nCarro: %d", j);
                    printf("\nModelo: %s", carro[j].modelo);
                    printf("\nFabricante: %s", carro[j].fabricante);
                    printf("\nCombustivel: %s", carro[j].combustivel);
                    printf("\nCor: %s", carro[j].cor);
                    printf("\nPlaca: %s", carro[j].placa);
                    printf("\nAno: %d", carro[j].ano);
                    printf("\nKm: %d", carro[j].km);
                    printf("\nPreco: %f\n", carro[j].preco);

                }
                j=j+1;
             }
            system("pause");
            system("cls");
            break;

        case 3:
            printf("Cor: ");
            fflush(stdin);
            fgets(cor, 10, stdin);
            j=*x;
             while (j<f){
                if (carro[j].cor==cor);{
                    printf("\nCarro: %d", j);
                    printf("\nModelo: %s\n", carro[j].modelo);
                }
                j=j+1;
             }
            system("pause");
            system("cls");
            break;

        default:
        system("cls");
        printf("Nao encontrado.\n\n");
        system("pause");
        system("cls");
        break;
    }

}

int main(){

struct fichacarro carro[49];
int n, x=0, y, cont, f=0;


printf("Numero de acoes: ");
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, &f);
        break;

    case 2:
        excluir(carro, &x, &f);
        break;

    case 3:
        pesquisar(carro, &x, f);
        break;

    case 4:
        x=n;
        break;

    default:
        system("cls");
        printf("Nao encontrado.\n\n");
        system("pause");
        system("cls");
        break;

    }
}

return 0;
}

1 answer

1


Although it is not clear what is the purpose of the variables x and fanalyzing the code they do not differ from value at any time. Start with value 0 in function main, are incremented together in the function inserir and decremented together in function excluir. That said, in function pesquisar the list of cars is iterated from xto f, and by having equal values it is impossible to enter the search loop while(j<f).

Browser other questions tagged

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