简体   繁体   English

如何将struct作为参数传递给其他函数进行输入,然后在原始函数处输出回来

[英]How to pass struct as parameter to other function for input, then output back at the original function

This is part of my project codes. 这是我的项目代码的一部分。

About the struct customer that I did, from welcomeScreen function I call the getInfo function for user to input the details (as you can see) and then return back the value to welcomeScreen function for output. 关于我所做的结构客户,从welcomeScreen函数调用了getInfo函数供用户输入详细信息(如您所见),然后将该值返回给welcomeScreen函数以进行输出。

I can compile the codes, but the problem is there is no output for all the details after I input it (just blank)? 我可以编译代码,但是问题是我输入所有细节后都没有输出(只是空白)? Sorry if this is a dumb question cause im still a student. 对不起,如果这是一个愚蠢的问题,因为我仍然是学生。

struct customer
{
    string name;
    string email;
    int number;
};
void welcomeScreen(); //prototype
void getInfo(struct customer cust); //prototype

void welcomeScreen()
{
    struct customer cust; // struct declaration
    const int SIZE=5;

    system("CLS");
    cout << setfill ('-') << setw (55) << "-" << endl;
    cout << "\tWelcome to Computer Hardware Shop" << endl;
    cout << setfill ('-') << setw (55) << "-" << endl;

    cout << endl << "Type of hardwares that we sell:" << endl;
    string item[SIZE]={"Monitor","CPU","RAM","Solid-State Drive","Graphic Card"};
    for(int i=0;i<SIZE;i++)
        cout << "\t" << i+1 << ". " << item[i] << endl;

    getInfo(cust);

    cout << endl;
    cout << fixed << showpoint << setprecision (2);
    cout << "Name: "<< cust.name << endl; // struct output
    cout << "Email: "<< cust.email << endl;
    cout << "Phone Number: " << cust.number << endl;
    cout << endl;
}

void getInfo(struct customer cust)
{
    cout << endl << "Enter name: ";
    cin >> cust.name;
    cout << "Enter email: ";
    cin >> cust.email;
    cout << "Enter phone number: ";
    cin >> cust.number; 
}

You probably want to pass a pointer or a reference, in this case recommend a reference because it means fewer changes to your code: 您可能想要传递一个指针或引用,在这种情况下,建议使用一个引用,因为这意味着对代码的更改更少:

void getInfo(struct customer &cust); //prototype

Remember to change your function parameter as well. 切记也要更改功能参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM