String comparison

Asked

Viewed 84 times

0

Is there any way I can find a specific letter in the string to enter this if of the special or normal account??

/*As contas dos clientes de um banco podem ser divididas em
contas “Comum” e “Especial”. Clientes com contas especiais possuem um campo para
o valor do limite, ao passo que contas do tipo Comum não. Além disso, todas as contas
devem conter: numero, nome do cliente e saldo. 
*/
#include <stdio.h>
#include <string.h>
#define tf 3

struct conta {
    int num_conta; 
    char tipo_conta [15];
    char nome[80];
    double saldo, limite; 
};


void leituracontas (struct conta vet[tf]){
    int i;
    for (i=0;i<tf;i++){

            printf ("\nEntre com o numero da conta [%d]: ", i);
            scanf ("%d", &vet[i].num_conta); 

            printf ("\nEntre com o tipo de conta [%d]: ", i); 
            fflush (stdin);
            fgets (vet[i].tipo_conta, 60, stdin);

            printf ("\nEntre com o nome [%d]: ", i);
            fflush (stdin);
            fgets (vet[i].nome,80,stdin);

            printf ("\nEntre com o saldo [%d]: ", i);
            scanf ("%lf", &vet[i].saldo);

            if (vet[i].tipo_conta == 'E')
            {
                printf ("\nEntre com o limite [%d]: ", i);
                scanf ("%lf", &vet[i].limite);
            }



                    }//fim do for

}//fim da funcao

void exibecontas (struct conta vet[tf]){
    int i; 
    for (i=0;i<tf;i++){

        printf ("\nNumero da conta [%d]: %d", i, vet[i].num_conta);

        printf ("\nTipo de conta [%d]: %s", i, vet[i].tipo_conta);

        printf ("\nNome [%d]: %s", i, vet[i].nome);

        printf ("\nSaldo [%d]: %.2lf", i, vet[i].saldo);

        if (vet[i].tipo_conta == 'E')
        {
            printf ("\nLimite [%d]: %.2lf", i, vet[i].limite);
        }

    }
}


int main (){
    struct conta nova[tf];
    int opcao = 0;

    while (opcao != 3){
        printf ("\n1 - Carregar o vetor de contas"); 
        printf ("\n2 - Exibe as contas"); 
        printf ("\n3 - Sair"); 
        printf ("\nQual a sua opcao? "); 
        scanf ("%d", &opcao); 

        switch (opcao){

            case 1: leituracontas (nova);
                break; 

            case 2: exibecontas (nova);
                break;

            default: 
            printf ("Opcao invalida!");
        }//fim do switch

    }//fim do while 

    return 0; 
}//fim do main
  • 1

    In your structure you have char type_account [15];, this is a string but in your test you consider it to be a single character and not a string: if (vet[i]. tipo_account == 'E'). To compare strings use the strcmp function of <string. h>. If you want to compare only the first character of the string use: if (vet[i]. fact_type[0] == 'E').

  • It worked :D Thanks d+ for the tip, I had never stopped to think of the string as indexes.

  • 1

    As index no, as array of characters with terminator ' 0' and yes, can be accessed through index.

1 answer

0


You can use the function strcmp library string.h.

Would look like this:

if( strcmp(vet[i].tipo_conta, "Especial") == 0)

This function returns 0 if the strings are equal.

Browser other questions tagged

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