Store data in STRUCT and print data on screen - 3 people

3

Can anyone tell me what I'm doing wrong? I need to store the data of 3 people in struct and then print it.

This error is appearing:

  

request for member 'name' in something not a structure or union

  request for member 'age' in something not a structure or union

  request for member 'weight' in something not a structure or union

  request for member 'height' in something not a structure or union

  request for member 'vector' in something not a structure or union |

But I did not understand what's wrong yet.

#include <stdio.h>
#include <stdlib.h>

int main() {

     struct pessoas {

        char nome[20];
        int idade;
        float peso;
        float altura;

    };

    struct pessoas usuario[3];

    int i;

    for (i=0; i<=2; i++)
    {

        printf("\n");
        printf("\nDigite seu nome: ");
        gets(usuario.nome);

        printf("\nDigite sua idade: ");
        scanf("%i", &usuario.idade);
        fflush(stdin);

        printf("\nDigite seu peso: ");
        scanf("%f", &usuario.peso);
        fflush(stdin);

        printf("\nDigite sua altura: ");
        scanf("%f", &usuario.altura);
        fflush(stdin);
    }

    for (i=0; i<=2; i++)
    {
      printf("\n%s, com %i anos voce pesa %.2f e tem %.2f de altura!", usuario.vetor[i], usuario[i].idade, usuario[i].peso, usuario[i].altura);

    }
    printf("\n\n");
    return(0);
}

Thank you in advance. ;)

    
asked by anonymous 17.09.2015 / 17:26

1 answer

3

In addition to using fgets() recommended by @pmg I found two errors in your code. First you make a vector of structs but do not access the elements of struct as vectors. Second at the time of displaying you pass as the first variable to be displayed usuario.vetor[i] that does not exist. To solve these problems do so:

#include <stdio.h>
#include <stdlib.h>

int main() {

     struct pessoas {

        char nome[20];
        int idade;
        float peso;
        float altura;

    };

    struct pessoas usuario[3];

    int i;

    for (i=0; i<=2; i++)
    {

        printf("\n");
        printf("\nDigite seu nome: ");
        fgets(usuario[i].nome, sizeof(usuario[i].nome), stdin);

        printf("\nDigite sua idade: ");
        scanf("%i", &usuario[i].idade);
        fflush(stdin);

        printf("\nDigite seu peso: ");
        scanf("%f", &usuario[i].peso);
        fflush(stdin);

        printf("\nDigite sua altura: ");
        scanf("%f", &usuario[i].altura);
        fflush(stdin);
    }

    for (i=0; i<=2; i++)
    {
      printf("\n%s, com %i anos voce pesa %.2f e tem %.2f de altura!",usuario[i].nome, usuario[i].idade, usuario[i].peso, usuario[i].altura);

    }
    printf("\n\n");
    return(0);
}

In this way the errors pointed out do not appear and the code compiles.

    
17.09.2015 / 18:07