gpt4 book ai didi

c++ - 在 C++ 中从文本文件读取输入到数组

转载 作者:行者123 更新时间:2023-11-28 03:54:43 24 4
gpt4 key购买 nike

<分区>

好吧,温和一点,因为我是编程的新手。到目前为止,我只学习了 C++,并且正在运行 Visual Studio 2010 作为我的编译器。对于这个程序,我试图从文本输入文件中读取信息并将信息写入一组三个数组。一个数组将处理姓名列表,另外两个数组分别用于处理工作时间和时薪。我将使用后两者来计算一组 yield 并将这些计算结果输出到另一个文本文件。然而,我的问题是获取第一个数组的输入。我正在使用的输入文件的文本排列如下:

J.母鹿* 35 12.50

J.黎明* 20 10.00…………

名称后面有星号,因为我正在尝试使用 ifstream getline 获取名称,其中星号作为分隔符,并将以下两个数字写入其他两个数组。后两个值由空格分隔,所以我认为它们不会造成任何问题。我确定还有其他错误需要处理,但我需要先解决第一个错误,然后才能开始调试其余错误。我在调用 inFile.getline 的那一行遇到错误,内容如下:

error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize,_Elem)' : cannot convert parameter 1 from 'std::string' to '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;
}

我已经包含了整个程序,以便您了解我计划在何处进行编码。在此先感谢您的帮助!

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com