这是我项目代码的一部分。
关于我做的结构客户,我从 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/