Axel Lutzky Marshall
Axel Lutzky Marshall

Reputation: 35

Adding numbers from a struct to a variable in my code

struct estudiante {
    string apellidos ;
    string nombres ;
    int anioIngreso ;
    int anioActual ;
    float promedio ;
} ;

struct recibido {
    string apellidos ;
    string nombres ;
    int anioEgreso ;
    float promedio ;
} ;

bool continuar () ;
bool tipoIngreso () ;
estudiante ingresoEstudiante () ;
recibido ingresoRecibido () ;
estudiante mayorPromedioE (estudiante mayorPromE , estudiante ingresoE) ;
recibido mayorPromedioR (recibido mayorPromR , recibido ingresoR) ;
void display (int cantidad , float sumaPromedios , estudiante mayorPromE , recibido mayorPromR) ;


int main () {
    int cantidad = 0 ;
    bool cont , tipo;
    float sumaPromedios ;
    estudiante ingresoE , mayorPromE = {"" , "" , 0 , 0 , 0};
    recibido ingresoR , mayorPromR = {"" , "" , 0 , 0};
    
    do {
        cantidad++ ;
        tipo = tipoIngreso () ;
        if (tipo = true) {
            ingresoE = ingresoEstudiante () ;
            mayorPromE = mayorPromedioE (mayorPromE , ingresoE) ;
            sumaPromedios += ingresoEstudiante.promedio ;   
        }
        else {
            ingresoR = ingresoRecibido () ;
            mayorPromR = mayorPromedioR (mayorPromR , ingresoR) ;
            sumaPromedios += ingresoRecibido.promedio ; 
        }
            
        cont = continuar () ;   
    } while (cont == true) ; 
    
    display(cantidad , sumaPromedios , mayorPromE , mayorPromR) ;
    
    return 0 ;
}

estudiante ingresoEstudiante () {
    estudiante ingreso ;
    
    cin.ignore() ;
    cout << "Ingrese los datos del estudiante" << endl ;
    
    do {
        cout << "Apellido: " ; getline(cin , estudiante.apellidos) ;
    } while (estudiante.apellidos == "") ;
    
    do {
        cout << "Nombre: " ; getline(cin , estudiante.nombres) ;
    } while (estudiante.nombres == "") ;
    
    do {
        cout << "Año de ingreso (posterior a 2011): " ; cin >> estudiante.anioIngreso ;
    } while (estudiante.anioIngreso <= 2011) ;
    
    do {
        cout << "Año actual (entre 1 y 5): " ; cin >> estudiante.anioActual ;
    } while (estudiante.anioActual < 1 and estudiante.anioActual > 5) ;
    
    do {
        cout << "Promedio: " ; cin >> estudiante.promedio ;
    } while (estudiante.promedio > 10) ;
    
    return ingreso ;    
}

The line that's giving me a problem right now is this one:

sumaPromedios += ingresoEstudiante.promedio ;

What I'm not sure is if you can't pick up a number from a struct (in this case the float promedio) and add it to a variable in main (sumaPromedios). Did i do something wrong or do i have to find a way around this?

The error the compiler is giving me is this one:

"[Error] request for member 'promedio' in 'ingresoEstudiante', which is of non-class type 'estudiante()'"

Upvotes: 0

Views: 61

Answers (1)

Sam Varshavchik
Sam Varshavchik

Reputation: 118292

estudiante ingresoEstudiante () ;

The shown code declares ingresoEstudiante to be a function that takes no parameters and returns an estudinate object. That's what this declaration means in C++.

sumaPromedios += ingresoEstudiante.promedio ; 

This line of course appears to access a member called promedio in some object called ingresoEstudiante. Except that it's not an object, but a function.

This is not the only bug in the shown code. I just happen to notice another one.

float sumaPromedios ;

This float value is declared, but not initialized to anything.

sumaPromedios += ingresoEstudiante.promedio ; 

Whether this is a function or an object, this attempts to add something to sumaPromedios. This sumaPromedios has never been initialized to anything, this is undefined behavior and after you fix the compilation error you will likely discover that the resulting value will be junk. You will need to fix this issue, as well.

Upvotes: 2

Related Questions