I can not get the values of an array by method of the initialized object

1

Good greetings, I have problems with recovering the values stored in the array, the drawback is that I'm not sure if double Nota::getNotas() and void Nota::inicializa() , are correct.

class Nota 
{
    public: 
        Nota();
        void iniciarAN(int Nalum, int Nntas);
        void inicializa(int array[], int idalum, int Nalum, int Nntas);
        double getNotas();
        int getAlumno();
        int getPromedio();
        int CalculaPromedio();
    private:
        int Notas[];
        int Nnotas; 
        int Nalumno; 
        int IDalumno;
        double promedio;
        int count;
};

Nota::Nota()
{ 
    Notas[20] =0;
    IDalumno = 0;
    promedio = 0;
    Nnotas = 0;
    Nalumno = 0;
}

// pone al objeto los valores recibidos como parámetro
void Nota::iniciarAN(int Nalum, int Nntas)
{ 
    Nnotas = Nalum;
    Nalumno = Nntas;
}

// pone al objeto los valores recibidos como parámetro
void Nota::inicializa(int array[], int idalum, int Nalum, int Nntas)
{
    Nnotas = Nalum;
    Nalumno = Nntas;
    Notas[Nntas] = array[Nntas];
    IDalumno = idalum;
}

// Regresa las notas
double Nota::getNotas()
{ 
    return Notas[Nnotas]; 
}

// Regresa la id del alumno
int Nota::getAlumno()
{
    return IDalumno;
}

// Regresa el promedio
int Nota::getPromedio()
{ 
    return promedio;
}

// Agrega cant a la cantidad de Notas en existencia
int Nota::CalculaPromedio()
{
    //promedio=0;
    for (int i = 0; i < Nnotas; ++i)
    {
        promedio+=Notas[i];
    }
    return promedio;
}

#include <iostream>
#include "Nota.h"

using namespace std;

Nota lista[30], AN;
int alm, nts;

void ListarNotas()
{
    cout<<"La lista de articulos en el almacen es la siguiente"<<endl;
    cout<<"\t"<<"No"<<"\t"<<"ID Alumno"<<"\t";
    for (int i = 0; i < alm; i++)
        cout<<"Nota "<<i+1<<"\t";
    cout<<"Promedio"<<endl;

    for (int i = 0; i < alm; i++)
    {
        cout<<"\t"<<i+1<<"\t"
        <<lista[i].getAlumno()<<"\t";
            for (int n = 0; n < nts; n++)
                cout<<lista[i].getNotas()<<"\t";
        cout<<lista[i].CalculaPromedio()<<endl;
        cout<<endl;
    }
}

int main()
{ 
    int opc, idalum, array[20];
    double pro;

    cout<<"Cantidad de Alumnos: ";
    cin>>alm;
    cout<<"Cantidad de Notas: ";
    cin>>nts;
    //AN.iniciarAN(alm,nts);
    for (int a = 0; a < alm; a++)
    {
        cout<<"Teclea el numero de identificacion del Alumno "<<a+1<<endl;
        cin>>idalum;
            for (int n = 0; n < nts; n++)
            {
                cout<<"Ingresa La Nota"<<n+1<<endl;
                cin>>array[n];
            }
        lista[a].inicializa(array, idalum, alm, nts); 
    }
    ListarNotas();
    return 0;
}

Results obtained.

Cantidad de Alumnos: 4
Cantidad de Notas: 3
Teclea el numero de identificacion del Alumno 1
1234
Ingresa La Nota1
12
Ingresa La Nota2
24
Ingresa La Nota3
20
Teclea el numero de identificacion del Alumno 2
10
Ingresa La Nota1
23
Ingresa La Nota2
24
Ingresa La Nota3
1
Teclea el numero de identificacion del Alumno 3
20
Ingresa La Nota1
12
Ingresa La Nota2
4
Ingresa La Nota3
5
Teclea el numero de identificacion del Alumno 4
21
Ingresa La Nota1
2
Ingresa La Nota2
3
Ingresa La Nota3
5
La lista de articulos en el almacen es la siguiente
        No      ID Alumno       Nota 1  Nota 2  Nota 3  Nota 4  Promedio
        1       1234    0       0       0       1241

        2       10      0       0       0       17

        3       20      0       0       0       27

        4       21      0       0       0       28


--------------------------------
Process exited after 23.83 seconds with return value 0
Presione una tecla para continuar . . .
    
asked by Mario P 03.09.2016 в 04:27
source

1 answer

2

Ok let's go there ... You have some errors. The first one is that you can not declare an array in a class and not give it the size when declaring it . If you want to size it later, you must use dynamic memory, a pointer and then define the size with new [] inside the constructor. Something like:

#include <iostream>
using namespace std;

class Foo{
public:
    Foo( const int TAM){ array = new int[TAM]; }
    ~Foo(){ delete[] array; }
private:
    int* array;
};

int main (){
    Foo bar(10);
    cin.ignore();
    return 0;
}

It is also very important not to forget to destroy with delete [] inside the destructor. Another solution is to give a fixed size at the time of declaring it the same as you would in main.

The Nota::getNotas function must be passed the index of the note you want to get:

int Nota::getNotas(**int i**) {
    return Notas[**i**];
}

And at the time of calling you do it by passing the value of the index such that:

for ( int n = 0; n < nts; n++ )
    cout << lista[i].getNotas(**n**) << "\t";

Another error that gave that function is that it returned a double but the correct thing is that you make it return an int (The type of the returned data). Then if you need to do a cast.

Another mistake that I saw was this line:

Notas[Nntas] = array[Nntas];

You can not copy an array to another, an array is not an object that contains member functions that do certain work to you, an array is just a set of data of the same type, if you want to copy it you must do it yourself or use some function for them. one way to copy an array to another can be this:

std::copy(array, array+20, Notas);

To use that function (copy) vc you must include. The function needs you to tell it the start of the array to be copied (array), the end of the array to be copied (array + 20 which is the size of array) and the start of the array that will receive the data (Notes). Another simpler way is to copy one by one with a for.

The complete code is here here .

    
answered by 03.09.2016 в 06:32