C code error

Asked

Viewed 83 times

3

#include <stdio.h>
typedef struct
    {
           char nome[10];
           float altura;
           float peso;
           char cpf[12];
           char sexo[10];
          }Dados;
void imc(Dados x);
char consulte[12];
void imc(Dados x[1])
{
    int i,c;
    for(i=0;i<=1;i++)
    {
    if (consulte==x.cpf)
    {
        if(x.peso<18.5)
        {
            printf("Baixo do peso.");
        }
        else
            {
                if((x.peso>=18.5) && (x.peso<25))
                {
                    printf("Peso adequado.");
                }
                else{
                    if((x.peso>=25) && (x.peso<30))
                    {
                        printf("Sobrepeso.");
                    }
                    else
                    {
                        if(x.peso>=30)
                        {
                            printf("Obesidade.");
                        }
                    }
                }
            }
    }
}
}
int main ()
{
    int i;
    char verdadeiro[10];
    Dados pessoas[1];
    for(i=0;i<=1;i++)
    {
        fflush(stdin);
        printf("Digite o nome da %i pessoa: ",i);
        scanf("%s", &pessoas.nome);
        fflush(stdin);
        printf("Digite a altura da %d pessoa: ",i);
        scanf("%f", &pessoas.altura);
        fflush(stdin);
        printf("Digite o cpf da %d pessoa: ",i);
        scanf("%s", &pessoas.cpf);
        fflush(stdin);
        printf("Digite o peso da %d pessoa: ",i);
        scanf("%f", &pessoas.peso);
        fflush(stdin);
        printf("Digite o sexo da %d pessoa: ",i);
        scanf("%s", &pessoas.sexo);
        fflush(stdin);
    }
    do
    {
        printf("Digite o nome do cpf da pessoa que você deseja ver o peso:");
        scanf("%s", &consulte);
        imc(pessoas[1]);
        printf("Você quer continuar? Digite[S/N]");
        scanf("%s", &verdadeiro);
    }
    while (verdadeiro!="N");
    return 0;
}

Description of the problem:

When running I cannot access the user by Cpf, in the code use if’s because I want possibilities for the imc. I don’t know what exactly is wrong.

I still think the mistake is in if, because I want to compare the (variable) con which is the same as the (variable) consulte which is where Cpf is that the user wants to consult con,with the x[i].cpf to see if Cpf is right.

At this point I compare I think it does not access the if and only asks if user wants to continue. And it does not show what I want to show the Imc.

  • When running I can’t access the user by Cpf, in the code I use if’s because I want possibilities for the imc. I don’t know what exactly is wrong.

  • Thanks for correcting my code, I was lost trying to fix.

  • I still think the error is in the if, because I want to compare the con that is the same as (variable)see that is where Cpf is that the user wants to query there compare the con,with x[i]. Cpf to see if Cpf is right. At this point I compare it does not access the if and only asks if user wants to continue.

  • Okay. I’ll edit it.

  • 2

    To compare strings (char array) in C you must use the strcmp function of <string. h> and not the operator ==.

  • 2

    Declare your imc function with Data parameter x[] and call such a function by informing the people argument (imc(people[]);).

  • Thank you, Anonimo. I was able to fix my code, thanks to you thanks for the help. Stay with God.

  • @anonymity create a response

Show 3 more comments

1 answer

1


In your first go, of registration, you initialize the value i 0 and do the repetition until i<=1... I understand that you want to register two people (people[0] and people[1]) is this?

From now on I don’t quite understand the code.

printf("Digite o nome do cpf da pessoa que você deseja ver o peso:");
scanf("%s", &consulte);

The user types the "person’s Cpf name"... If I understood the purpose of the algorithm, I think Voce wanted through the data entry to do a search in the register, is that it? If it is, do something like this:

 do
 {
        printf("Digite o cpf da pessoa que você deseja ver o peso:");
        scanf("%s", &consulte);
        //percorre o cadastro procurando pela pessoa com cpf = consulta
        for(int i=0; i <=1 && !strcmp(consulte, pessoas.cpf)==0;i++);
        imc(pessoas[i]);

        printf("Você quer continuar? Digite[S/N]");
        scanf("%s", &verdadeiro);
 }while (verdadeiro!="N");

In the function imc Voce receives the variable thus:

void imc(Dados x[1])

If Voce is only dealing with one person it is not necessary to create this vector. Do so:

void imc(Dados x)

Within the imc function there are comparisons of strings, whenever you manipulate them use the #include<string.h>, in this case, comparisons are made as follows::

if(strcmp(string1, string2)==0) //a string1 é igual a string2? Se for ele executa o if

I believe this can solve most of your problems.

Browser other questions tagged

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