好吧,温柔点,因为我是编程新手。到目前为止,我只学习了 C++,并且正在运行 Visual Studio 2010 作为我的编译器。对于这个程序,我试图从文本输入文件中读取信息并将信息写入一组三个数组。一个数组将处理姓名列表,另外两个数组分别用于工作时间和小时工资率。我将使用后两者来计算一组收益,并将整个结果作为报告输出到另一个文本文件中。然而,我的问题是获取第一个数组的输入。我正在使用的输入文件的文本排列如下:
J. Doe* 35 12.50 J. Dawn* 20 10.00 .........
这些名称后面带有星号,因为我正在尝试使用 ifstream getline 来获取带有星号作为分隔符的名称,并将以下两个数字写入其他两个数组。后两个值由空格分隔,所以我认为它们不会引起任何问题。我确定还有其他需要处理的错误,但我需要先解决第一个错误,然后才能开始调试其余错误。
我的问题出现在我调用 inFile.getline 的行上,因为我收到以下错误:error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std ::streamsize,_Elem)' : 无法将参数 1 从 'std::string' 转换为 'char *'。
从我在其他地方读到的内容来看,(我认为)问题源于试图将字符串写入 char 数组,因为它们具有不同的数据类型,所以这不起作用。我不确定是否存在其他可行的方法来获取名称,因为我需要分隔符将名称与数值分开。任何有关如何解决此问题的建议将不胜感激。
这是我写的源代码:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int EMP_NUM = 5;
const int BASE_HOURS = 40;
const char N_SIZE = 8;
int main()
{
int i;
double rEarnings, oEarnings, tEarnings,
trEarnings, toEarnings, ttEarnings;
ifstream inFile;
ofstream outFile;
inFile.open("records.txt");
outFile.open("report.txt");
outFile << setprecision(2) << showpoint << fixed;
outFile << setw(50) << "Payroll Report" << "\n\n";
outFile << "EMPLOYEE NAME" << setw(25) << "REGULAR EARNINGS" << setw(25) << "OVERTIME EARNINGS" << setw(25) << "TOTAL EARNINGS" << endl;
string nameAr[EMP_NUM];
int hoursAr[EMP_NUM];
double hrateAr[EMP_NUM];
for (int i = 0; i < EMP_NUM; i++) // Get input from our input file.
{
inFile.getline(nameAr[i], EMP_NUM, "*");
inFile >> hoursAr[i] >> hrateAr[i];
}
for (int i = 0; i < EMP_NUM; i++) // Make the calculations to be sent to our report.
{
char nameAr[N_SIZE];
int hoursAr[N_SIZE];
double hrateAr[N_SIZE];
if (hoursAr[i] > 40) // For employees with overtime hours.
{
// double rEarnings, double oEarnings, double tEarnings,
// double trEarnings, double toEarnings, double ttEarnings;
// rEarnings = 0, oEarnings = 0, tEarnings = 0,
// trEarnings = 0, toEarnings = 0, ttEarnings = 0;
rEarnings = BASE_HOURS * hrateAr[i];
oEarnings = (hoursAr[i] - BASE_HOURS) * hrateAr[i] * 1.5;
tEarnings = rEarnings + oEarnings;
trEarnings += rEarnings;
toEarnings += oEarnings;
ttEarnings += tEarnings;
outFile << left << nameAr[i];
// << setw(25) << right << rEarnings << setw(25) << right << oEarnings << setw(25) << right << tEarnings << endl;
}
else // For employees without overtime hours.
{
// double rEarnings, double oEarnings, double tEarnings,
// double trEarnings, double toEarnings, double ttEarnings;
// rEarnings = 0, oEarnings = 0, tEarnings = 0,
// trEarnings = 0, toEarnings = 0, ttEarnings = 0;
rEarnings = hoursAr[i] * hrateAr[i];
oEarnings = 0;
tEarnings = rEarnings + oEarnings;
trEarnings += rEarnings;
toEarnings += oEarnings;
ttEarnings += tEarnings;
outFile << left << nameAr[i];
// << setw(25) << right << rEarnings << setw(25) << right << oEarnings << setw(25) << right << tEarnings << endl;
}
}
outFile << endl << endl;
outFile << setw(33) << trEarnings << " *" << setw(23) << toEarnings << " *" << setw(23) << ttEarnings << " *\n\n";
outFile << left << "TOTAL EMPLOYEES" << " " << (i - 1);
inFile.close(); outFile.close();
return 0;
}
我已经包含了整个程序,让您了解我打算在哪里进行编码。在此先感谢您的帮助!