I can not compare the strings

-1

I registered the client code and now I need to request again, so I need to check whether or not there is a registered code, but even if the codes are different, it says that the code has been registered.

Issue: 3 - Calculate the value of the sale of the vehicle Request: Vehicle plate (check if you have registered vehicle) Customer code (check if you have registered customer) The factory value of the vehicle Check if the customer wants to buy the vehicle in sight or in 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;     
}
    
asked by anonymous 26.11.2018 / 23:00

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" relative to the second "string". (This minor idea 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".

In short, if the first string is less than 0 and less than the second string, if it is greater than 0 the first string and greater than the second string. And if it is 0 both are equal.

I suggest you change 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 on the contrary, you were checking if the value was different and returning that it was the same if it were different, this way if strcmp return 0, that is, they are the same, ai he printa "has code registered", if the flaw is! = 0 then it goes to the else.

I hope I have been succinct and that the answer will help you.

    
26.11.2018 / 23:47