-4

I was trying to fill and show the data of a struct using void functions, the problem is that it looks that there is a problem once I try to fill the struct "persona" with the void function "llenar", it does not fill it, once I show the data (with "mostrar") in the console it looks like it is empty, this problem does not appear when I do not use the void function "llenar".

#include <iostream>
using namespace std;

struct persona
{
    string nombre; //elementos
    float fisica;
    float quimica;
    float matematica;
    float ponderado;
};

void llenar (persona P)
{
    cout<<"nombre: ";
    cin>>P.nombre;
    cout<<" nota fisica: ";
    cin>>P.fisica;
    cout<<" nota quimica: ";
    cin>>P.quimica;
    cout<<" nota matematica: ";
    cin>>P.matematica;

}
void mostrar(persona P)
{
    cout<<"nombre: ";
    cout<<P.nombre<<endl;
    cout<<" nota fisica: ";
    cout<<P.fisica<<endl;
    cout<<" nota quimica: ";
    cout<<P.quimica<<endl;
    cout<<" nota matematica: ";
    cout<<P.matematica<<endl;
}

int main() 
{

    int C;
    //float Po;
    cout<<"Enter the number of people: ";
    cin >> C;
    persona * P1;
    P1 = new persona [C];


    for(int i = 0 ; i<C ; i++)
    {
        cout<<"Person "<<i+1<<" :"<<endl;
        llenar(P1[i]);
        // NOT USING void "llenar"
        /*
        cout<<"Igresa nombre: ";
        cin>>P1[i].nombre;
        cout<<"Igresa nota fisica: ";
        cin>>P1[i].fisica;
        cout<<"Igresa nota quimica: ";
        cin>>P1[i].quimica;
        cout<<"Igresa nota matematica: ";
        cin>>P1[i].matematica;
        */
        mostrar(P1[i]);

    }

    return 0;
}

To make a long story short, does someone knows how to fill a struct like "persona" from a void function?

user4581301
  • 33,082
  • 7
  • 33
  • 54
JPgiq
  • 74
  • 9
  • 1
    Helpful reading: [What's the difference between passing by reference vs. passing by value?](https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) – user4581301 Jun 11 '18 at 04:25

1 Answers1

1

You have to pass the argument by reference, so that it can be modified:

void llenar (persona P);  // becomes ==>
void llenar (persona &P);
Kostas
  • 4,061
  • 1
  • 14
  • 32