0

Can someone help me? the error is in the research which is "pesquisar" in portuguese. I just wanna know why it doesn't work. The code is not printing the search result. Any help is good. The code is in portuguese but I think it won't be a problem. Thank u all.

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); // <---it doesn't work
                    printf("\nModelo: %s\n", carro[j].modelo);// <---it doesn't work
                }
                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); // <---it doesn't work
                    printf("\nModelo: %s", carro[j].modelo); // <---it doesn't work
                    printf("\nFabricante: %s", carro[j].fabricante);// <---it doesn't work
                    printf("\nCombustivel: %s", carro[j].combustivel); // <---it doesn't work
                    printf("\nCor: %s", carro[j].cor); // <---it doesn't work
                    printf("\nPlaca: %s", carro[j].placa); // <---it doesn't work
                    printf("\nAno: %d", carro[j].ano); // <---it doesn't work
                    printf("\nKm: %d", carro[j].km); // <---it doesn't work
                    printf("\nPreco: %f\n", carro[j].preco); // <---it doesn't work

                }
                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); // <---it doesn't work
                    printf("\nModelo: %s\n", carro[j].modelo); // <---it doesn't work
                }
                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;
}
Ygor Fraga
  • 143
  • 1
  • 1
  • 10
  • 1
    There's a few things you could do to improve the question. 1) Formatting your code. It's difficult enough to read as it's in Portuguese. 2) Include the error messages/symptoms. Things like "it doesn't work" *do not help* – Tibrogargan Oct 02 '16 at 01:25
  • 2
    `fflush(stdin)` is undefined behaviour. Also see [ask], provide a [mcve]. We are not a debugging service. – too honest for this site Oct 02 '16 at 01:25
  • Right, I'll do it. I'm just learning how to use stackoverflow. I'll improve it. Thank guys. – Ygor Fraga Oct 02 '16 at 01:29
  • I did not understand about the bevabiour of fflush(stdin), why is it undefined? – Ygor Fraga Oct 02 '16 at 01:31
  • Why has a M3 screw 3mm diameter? Because that's what the standard says. – too honest for this site Oct 02 '16 at 01:32
  • how should I use fflush(stdin)? – Ygor Fraga Oct 02 '16 at 01:36
  • 1
    You shouldn't. fflush forces a write. You can't write to stdin. You probably mean fflush(stdout) – Tibrogargan Oct 02 '16 at 01:38
  • 1
    See [Using `fflush(stdin)`](http://stackoverflow.com/questions/2979209/using-fflushstdin) for a nuanced explanation of the story. The C standard says `fflush(stdin)` is undefined behaviour; the POSIX standard gives it a limited meaning; the Microsoft runtime library gives it a useful but non-standard defined meaning — as is the prerogative of any implementation. Since you don't indicate where you plan to run your code, it is best to avoid using it. – Jonathan Leffler Oct 02 '16 at 05:00
  • Compile with symbols, run the code inside a debugger, tracing through the program while inspecting all relevant variables to learn what is really going on. – alk Oct 02 '16 at 14:48

0 Answers0