【发布时间】:2015-06-23 05:57:18
【问题描述】:
我很困惑我会在我的函数中放入什么样的变量:names。我在 C++ 书中做一个练习题,因为我正在学习 C++,并且现在在参考和指针上,并且找不到解决方案。
只是为了背景信息,问题问:
编写一个函数,提示用户输入他或她的名字和姓氏,作为两个单独的值。
此函数应通过传递给函数的附加指针(或引用)参数将这两个值返回给调用者。
尝试先使用指针,然后使用引用。
#include <iostream>
#include <string>
#include <istream>
using namespace std;
struct someStruct{
string firstname;
string lastname;
};
void names(someStruct &firstname, someStruct &lastname) {
cout << "First Name: " << "\n";
cin >> firstname.firstname;
cout << "Last Name: " << "\n";
cin >> lastname.lastname;
// I was just curious is adding firstname to firstname would work... and it did
cout << lastname.lastname << ", " << firstname.firstname;
cin.get();
}
int main() {
names();
// I don't know what to put here, above, as parameters
cin.get();
}
【问题讨论】:
-
names()带指针你知道怎么做吗?