Problem with Struct

Asked

Viewed 122 times

0

I have the following error in my code:

error: request for Member 'media' in 'given', wich is of non-class type 'Dadosaluno [5]'

Code:

#include <iostream>
using namespace std;

struct DadosAluno{
        int idade[5];
        float media[5];
};

int main (){

    struct DadosAluno dados[5];
    int i;
    for (i=0; i<5; i++){
        cout<< "Digite a idade do aluno: ";
        cin>> dados.idade[i]; //O ERRO ESTÁ AQUI 
        cout<< "Digite a média do aluno: ";
        cin>> dados.media[i];
    }
    cout<< endl;
    cout<< "Dados dos alunos:" << endl;
    for (i=0; i<5; i++){
        cout<< "Idade: " << dados.idade[i] << endl;
        cout<< "Média: " << dados.media[i] << endl;
    }

    return 0;
}

1 answer

5

What you want is a student with five ages and five averages or five students each with an age and an average?


If you want a student with five ages and five averages:

So the problem is on this line:

struct DadosAluno dados[5];

That should be it:

struct DadosAluno dados;

If what you want is five students each with an age and an average:

Then your struct is wrong. Instead:

struct DadosAluno{
    int idade[5];
    float media[5];
};

You should wear this:

struct DadosAluno {
    int idade;
    float media;
};

And in the places where you use dados.idade[i] should be dados[i].idade. Where it uses dados.media[i] should be dados[i].media.


If what you wanted is neither of the above two things, then please explain what you are trying to do.

Also, your error message does not exactly match the code, since the variable is called dados, but the error message refers to a variable dado (without the s).

Browser other questions tagged

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