Problem with Struct

0

I have the following error in my code:

  

error: request for member 'media' in 'given', wich is of non-class type   'DataAluno [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;
}
    
asked by anonymous 13.02.2015 / 06:33

1 answer

5

What do 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 means:

So the problem lies in this line:

struct DadosAluno dados[5];

What should be this:

struct DadosAluno dados;

If you want 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 use this:

struct DadosAluno {
    int idade;
    float media;
};

And wherever 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 not one of the two things above, then please explain what you are trying to do.

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

13.02.2015 / 06:48