cannot compare strings

Asked

Viewed 102 times

-1

I registered the code of the client and now I need to request again so I need to check whether there is or not registered code, only even if the codes are different or says that there is registered code.

Question: 3 - Calculate the sale value of the vehicle Request: Vehicle plate (check if you have a registered vehicle) Customer code (check if you have a registered customer) The factory value of the vehicle Check if the customer wants to buy the vehicle on demand or on time

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include <string.h>
main (){

    char aux[50];
    int opcao;

    printf("################################");printf("\n#       seja bem vindo         #");printf("\n#                              #"); printf("\n################################"); 
    struct cliente
    {
        char codigo[50];
        char nome[50];
        char endereco[50];
        float salario;

    };

    struct cliente cadastro;
    cadastro.codigo;
    cadastro.nome;
    cadastro.endereco;
    cadastro.salario;
    struct veiculo
    {
        char placa[50];
        char marca[50];
        char ano[50];
        char combustivel[50];
    };

    struct veiculo cadastro1;
    cadastro1.placa;
    cadastro1.marca;
    cadastro1.ano;
    cadastro1.combustivel;

    do{
        printf("\n1 - Cadastrar Novo Cliente");
        printf("\n2 - Cadastro Novo Veiculo");
        printf("\n3 - Calcular o Valor Da Venda Do Veiculo");
        printf("\n0 - Finalizar\n");
        printf(" \nSelecione uma opcao por favor: ");
        scanf("%d", &opcao);
        fflush(stdin);

        if(opcao==1){
            system("cls");
            printf("#######################################################");printf("\n# Voce selecionou a opcao 1 - Cadastrar Novo Cliente  #");printf("\n#                                                     #"); printf("\n#######################################################"); 

            printf("\n\nDigite o codigo Para o cliente: ");
            gets(cadastro.codigo);
            fflush(stdin);

            printf("\nDigite o Nome Do Cliente: ");
            gets(cadastro.nome);
            fflush(stdin);

            printf("\nDigite o Endereco Do Cliente: ");
            gets(cadastro.endereco);
            fflush(stdin);

            printf("\nDigite o Salario Do Cliente: ");
            scanf("%.2f",cadastro.salario);
            printf("\n\n");
            fflush(stdin);

            printf("\n\nCliente Cadastrado\n\n");
            fflush(stdin);

            printf(" \nPrecione Enter Para Volta ao Menu Principal.... ");
            getchar();
            system("cls");
            main();
        }
        else{
            system("cls");
            if(opcao==2){
                printf("#######################################################");printf("\n# Voce selecionou a opcao 2 - Cadastro Novo Veiculo   #");printf("\n#                                                     #"); printf("\n#######################################################"); 

                fflush(stdin);
                printf("\n\nDigite a Placa do veiculo: ");
                gets(cadastro1.placa);
                fflush(stdin);

                printf("\nDigite a Marca Do Veiculo: ");
                gets(cadastro1.marca);
                fflush(stdin);

                printf("\nDigite o Ano Do Veiculo:");
                gets(cadastro1.ano);
                fflush(stdin);

                printf("\nDigite o Tipo de Combustivel: ");
                scanf("%c",cadastro1.combustivel);
                fflush(stdin);


                printf("\n\nVeiculo Cadastrado\n\n");
                fflush(stdin);

                printf(" \nPrecione Enter Para Volta ao Menu Principal.... ");
                getchar();
                system("cls");
                main();
                break;
            }
            else{
                if(opcao==3){              
                    printf("\n\n insira o codigo do cliente");
                    gets(aux);
                    if (strcmp(aux,cadastro.codigo))
                    printf ("\n\n  ha codigo cadastrado");
                    else printf ("\n\ nao ha codigo cadastrado.");
                    printf(" \nPrecione Enter Para Volta ao Menu Principal.... ");
                    getchar();
                    system("cls");
                    main();
                    break;
                }
            }
        }   

    }while (opcao != 9 || opcao < 9);

    return 0;     
}

1 answer

0

Your error is in the way you are using the strcmp function.

This function has this signature:

int strcmp ( const char * str1, const char * str2 );

Soon it returns an int.

How it works: it returns three possible values.

"<" 0 (less than zero) - if the first different character in the two "strings" is smaller in the first "string" than the second "string". (This idea of minor is related to the values of each character in the ASCII table)

0 - if the "strings" are equal

">" 0 (greater than zero) if the first different character in the two "strings" is greater in the first "string" than the second "string".

Summarizing if it is less than 0 the first "string" and less than the second, if it is greater than 0 the first "string" and greater than the second. And if it’s 0 Both are equal.

I suggest changing your if to:

if (!strcmp(aux,cadastro.codigo)) // 0 é corrospondente a false e ! é basicamente if (strcmp(aux,cadastro.codigo) == false)
                    printf ("\n\n  ha codigo cadastrado");

Your if was the other way around, you were checking if the value was different and returning that it was the same if it was different, that way if strcmp returns 0, that is, they are the same, then it prints "ha registered code", if Falor is != 0 then he goes to Isis.

I hope I’ve been succinct and the answer helps you

  • then, it seems that strcmp is not pulling the struct string because even with the above mentioned code it did not run , even without registering code it tells how there is code

  • Strange because I tested it here and it worked normally, and looking I see no more errors related to this excerpt

  • you can send me your code just the if part to parse ? which compiler is using.

  • i do not use Windows use linux, compile directly by terminal with gcc, the if ta the same way I put there

  • Um, without the whirlwind

Browser other questions tagged

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