0

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

J. Doe* 35 12.50

J.黎明* 20 10.00 ......

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

错误 C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize,_Elem)' : 无法将参数 1 从 'std::string' 转换为'字符 *'。

从我在其他地方读到的内容来看,(我认为)问题源于试图将字符串写入 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;
}

我已经包含了整个程序,让您了解我打算在哪里进行编码。在此先感谢您的帮助!

4

2 回答 2

1

我不会getline以这种方式使用,因为您有两个单独的分隔符要处理 - 空格和星号。这就是我会做的事情:

用于getline将 FULL 行放入string被调用的line.

使用查找星号line.find('*');

string将名称提取为新名称,line.substr直至找到刚刚找到的位置

将星号之外的另一个substr变为stringstream被调用的remainder

使用 直接从中读取intand :doubleoperator>>remainder >> hours >> rate;

于 2010-11-05T18:54:09.017 回答
1

你好 C++ 新程序员!欢迎来到 C/C++ 编码的美妙之处。

我知道你刚刚开始使用 C++。但是要解决您的问题,我们必须接触一点 C。C++ 恰好是 C 的超集。这意味着可以在 C 中完成的所有操作都可以在 C++ 程序中运行。

好吧,足够的话,代码时间。将用于从输入文件中获取输入的代码替换为:

char tmp[256];
memset(tmp, '\0', sizeof tmp);
inFile.getline(tmp, EMP_NUM, '*');
nameAr[i] = tmp;
inFile >> hoursAr[i] >> hrateAr[i];

让我们来看看它。

char tmp[256];将创建一个临时数组以读取值。此数组的大小可能会随您收到的姓名的平均长度而变化。

Strings in C are like high maintenance chics. You have to specify a NULL character '\0' at the end or they may crash your program with not-so-obvious Segmentation faults. However, Memset is a little man that works down in the mines of a computer; he's gonna help us fix this. When called in this form memset(tmp, '\0', sizeof tmp), memset starts at the address of tmp, walks through all the bits of the array - the size id specified as sizeof tmp - and sets these bits to the character specified - in this case NULL. This way we would not have to remember to append the NULL character every-time we read in a C string; provided the size of tmp is sufficiently large. Convenient!

inFile.getline(tmp, EMP_NUM, '*'); reads the input [string] from your file as expected and stores it in tmp.

nameAr[i] = tmp; puts the input read into the names array.

and finally, inFile >> hoursAr[i] >> hrateAr[i]; reads the hours and hourly rates as before.

Hope this helps.

Cheers! Happy Learning.

于 2010-11-06T08:19:00.160 回答