我似乎无法获得“满员” 显示第一个和最后一个的串联。 它编译但是当我运行它时它将显示为空白。 你能告诉我为什么吗? 试着把它弄清楚几个小时。
以下是我的陈词滥调
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#define tax 0.30
#define parking_deductions 10.00
#define overtime_hours 40
#define max_hours 60
#define max_pay 99
string namestring( string first, string last, string full);
我正在尝试将此模块传递给我的主要
string namestring(string first, string last, string full)
{
//input name
cout << "What is your first name? " << endl;
cout << "first name: " << endl;
cin >> first;
cout << "What is your last name? " << endl;
cout << "last name: " << endl;
cin >> last;
//process name
full = last + " " + first;
return full;
}
通过这样称呼
namestring(first, last, full );
我期望用户输入的全名显示在
下面 cout << left << fixed << " " << "Reg." << " " << " Ovt." << " Hourly" << " Net" << " " << " Gross" << endl;
cout << left << fixed << setprecision(2) << setw(10) << "Name " << " Hours" << " Hours" << " Rate" << " Pay" << " Taxes" << " Deduct" << " Pay" << endl;
cout << left << fixed << setprecision(2) << setw(10) << "====================" << " " << "=====" << " " << "=====" << " " << "=====" << " " << "======" << " " << "======" << " " << " " << "========" << " " << "=======" << endl;
cout << left << setprecision(2) << setw(20) << full << right << " " << right << setw(4) << hours << right << " " << right << overtime << " " << right << pay << " " << right << net_pay << " " << right << taxs << " " << right << parking_deductions << " " << right << gross_pay << right << endl;
cout << endl;
cout << endl;
答案 0 :(得分:2)
我假设这里的目标是获得以下3个字符串,
First Name
Last Name
Full Name
为此,您需要通过引用而不是值传递参数:
void namestring(string& first, string& last, string& full)
{
//input name
cout << "What is your first name? " << endl;
cout << "first name: " << endl;
cin >> first;
cout << "What is your last name? " << endl;
cout << "last name: " << endl;
cin >> last;
//process name
full = last + " " + first;;
}
如果您通过引用传递“完整”字符串,也不需要返回字符串,因为该函数将为您填充。
答案 1 :(得分:1)
string namestring(string first, string last, string& full)
//input name
{
cout << "What is your first name? " << endl;
cout << "first name: " << endl;
cin >> first;
cout << "What is your last name? " << endl;
cout << "last name: " << endl;
cin >> last;
//process name
full = last + " " + first;
return full;
}
您按价值传递full
。所以它是被修改的函数的本地副本。你需要它通过引用传递。
如果您还想要第一个和最后一个值,您还需要通过引用传递它。