【问题标题】:Filling struct with void function用 void 函数填充结构
【发布时间】:2018-06-11 03:23:14
【问题描述】:

我试图使用 void 函数填充和显示结构的数据,问题是,一旦我尝试用 void 函数“llenar”填充结构“角色”,它看起来就有问题,它确实不填充它,一旦我在控制台中显示数据(带有“mostrar”)它看起来像是空的,当我不使用void函数“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;
}

长话短说,有人知道如何从 void 函数填充像“角色”这样的结构吗?

【问题讨论】:

标签: c++ function struct void


【解决方案1】:

您必须通过引用传递参数,以便对其进行修改:

void llenar (persona P);  // becomes ==>
void llenar (persona &P);

【讨论】:

    猜你喜欢
    • 2019-07-20
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    相关资源
    最近更新 更多