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

标签 c++ arrays input fstream getline

<分区>

好吧,温和一点,因为我是编程的新手。到目前为止,我只学习了 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;
}

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

最佳答案

你好 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]; 将创建一个临时数组以读取值。该数组的大小可能会随着您收到的名称的平均长度而变化。

C 中的字符串就像是高维护性的别致。您必须在末尾指定一个 NULL 字符 '\0' ,否则它们可能会导致您的程序因不太明显的段错误而崩溃。然而,Memset 是一个在计算机矿井 中工作的小人物;他会帮我们解决这个问题。当以 memset(tmp, '\0', sizeof tmp) 形式调用时,memset 从 tmp 的地址开始,遍历数组的所有位——大小 id 指定为 sizeof tmp - 并将这些位设置为指定的字符 - 在本例中为 NULL。这样我们就不必在每次读取 C 字符串时都记住附加 NULL 字符;如果 tmp 的大小足够大。方便!

inFile.getline(tmp, EMP_NUM, '*'); 按预期从文件中读取输入 [string] 并将其存储在 tmp 中。

nameAr[i] = tmp; 将读取的输入放入名称数组。

最后,inFile >> hoursAr[i] >> hrateAr[i]; 像以前一样读取小时数和小时费率。

希望这对您有所帮助。

干杯! 快乐学习。

关于c++ - 在 C++ 中从文本文件读取输入到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4108971/

相关文章:

python - 将 JSON 对象添加到列表中

python - 输入数字500以下的最大幂

javascript - 更改文本框 Javascript 的单个值

c++ - 具有基于 protected 基类方法的返回类型的函数

iOS swift : Array of Range

c++ - 你如何在C++中将两个字符串写入一个文件

javascript - 索引的值使用循环插入到下一行

java - 将文件夹内的所有文件作为函数的输入传递

c++ - C++中的手动内存管理

c++ - 如何在我自己的代码中使用 _DEBUG_ERROR?