c++ - 如何将结构作为参数传递给其他函数进行输入,然后在原始函数处输出

标签 c++ function struct

这是我项目代码的一部分。

关于我做的结构客户,我从 welcomeScreen 函数调用 getInfo 函数让用户输入详细信息(如您所见),然后将值返回给 welcomeScreen 函数进行输出。

我可以编译代码,但问题是我输入后没有输出所有细节(只是空白)?抱歉,如果这是一个愚蠢的问题,因为我还是一名学生。

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; 
}

最佳答案

您可能想传递一个指针或一个引用,在这种情况下推荐一个引用,因为这意味着对您的代码的更改更少:

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

请记住也要更改您的函数参数。

关于c++ - 如何将结构作为参数传递给其他函数进行输入,然后在原始函数处输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53634427/

相关文章:

c++ - 空 vector 允许推回其自身的第一个元素

C++11 未定义的函数引用

c++全局函数与根据内存的数据成员函数

c - 将 typedef 函数指针作为参数传递问题

c - 尝试为结构中的字符串分配内存时出现段错误

c++ - 我如何调用一个函数,它在一个单独的类中打印出一组结构?

c - 将结构数组分配给指针

C++程序陷入循环

c++ - 如何将 cmd 命令输出传递给 QMake 中的#DEFINE MACRO

c++ - C++技术建议中多线程环境中暂停和恢复一个线程从另一个线程