Media using class c++

Hi! I would like to know why the return value of media() is random!
Thanks!






#include <iostream>
#include<string>

using namespace std;

class aluno
{

string nome;
string curso;
int notas[5];

public:
void setvalue(int a, int b, int c, int d, int e, int f);

double media();
};

void aluno::setvalue(int a, int b, int c, int d, int e, int f){

a=notas[1];
b=notas[2];
c=notas[3];
d=notas[4];
e=notas[5];
f=notas[6];

}
double aluno:: media(){

int soma=0;
for(int i=0;i<5;i++){

soma=soma+notas[i];

}
return(soma/6);
}

int main()
{
aluno joao;
joao.setvalue(4,6,4,6,9,5);

cout << joao.media();
}
notas has index range 0 .. 4. You've indexed past that range and ate overwriting memory.
Thanks!
Topic archived. No new replies allowed.