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

Asked

Viewed 9,288 times

3

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

This error is appearing:

request for Member ːname' in Something not a Structure or Union|
request for Member ːidade' in Something not a Structure or Union|
request for Member ːpeso' in Something not a Structure or Union|
request for Member ːaltura' in Something not a Structure or Union|
request for Member ːvetor' in Something not a Structure or Union|

But I don’t 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);
}

From now on, thank you. ;)

  • usuario is an array: it has elements. It’s not a struct, it has no members. I suppose you want anything like usuario[0].nome. Oh! Never use it gets(). It is impossible to use safely and there is an alternative function that you can safely use: fgets()

  • Ah, ok. I’ll start using fgets() so thank you. ;)

1 answer

3


In addition to the use of fgets() recommended by @pmg I found two errors in your code. First you make a vector of structs but does 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);
}

This way the pointed errors do not appear and the code compiles.

  • I get it... thank you very much, I’ll try to do it this way here. ;)

  • I tried here, but when he comes back the second time in the gone he doesn’t read the name, goes straight to age. :/

  • So, after I inserted a whole number he would come back in the for and jump straight asking the age, I just managed to solve this by putting a getchar(); after the last scanf.

Browser other questions tagged

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