【发布时间】:2013-09-13 05:40:03
【问题描述】:
我想传递一个结构来实现类似下面的功能(我知道我可以将单个成员传递给像 input(int age, string s) 这样的功能,但我想像 input(student s) 一样传递整个结构)
#include <iostream>
using namespace std;
struct student
{
string name;
int age;
};
void input(student s)
{
cout << "Enter Name: ";
cin >> s.name;
cout << "Enter age: ";
cin >> s.age;
}
int main(int argc, char *argv[]) {
struct student s1;
input(s1);
cout << "Name is: " << s1.name << endl;
cout << "Age is: " << s1.age << endl;
}
上面的代码没有产生正确的输出,我想用上面的代码和指针来获得预期的输出。
测试:如果我输入姓名为“abc”,年龄为 10。它不会在 main 中打印
【问题讨论】:
-
出现错误或输出?
-
@shobi 如果我将名字输入“abc”并将年龄输入到 10。它不会在 main 中打印
-
请检查答案:)