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

发布于 2024-10-01 00:53:19 字数 3783 浏览 4 评论 0原文

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

J. Doe* 35 12.50

J. Dawn* 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 *'。

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

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

Alright, be gentle, since I'm very much a novice to programming. So far I've only studied C++ and I'm running Visual Studio 2010 as my compiler. For this program, I'm trying to read from a text input file and write the information to a set of three arrays. One array will handle a list of names, and the other two are for hours worked and hourly pay rate, respectively. I will use the latter two to calculate a set of earnings and output those calculations to another text file. My problem, however, is with acquiring input for the first array. The input file I'm using has text arranged like this:

J. Doe* 35 12.50

J. Dawn* 20 10.00
.........

The names are trailed by asterisks since I'm trying to use ifstream getline to acquire the names with the asterisks acting as delimiters, and writing the following two numbers into the other two arrays. The latter two values are separated by whitespaces, so I don't think they'll cause any problems. I'm sure there are other errors that need handling but I need to work through the first error before I can start debugging the rest. I encounter an error with the line where I call inFile.getline, which reads as follows:

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 *'.

From what I've read elsewhere, (I think) the problem stems from trying to write a string to a char array, which won't work since they are of different data types. I'm not sure if other feasible methods exist for acquiring the names since I need the delimiter to separate the names from the numerical values. Any advice on how to resolve this issue would be much appreciated.

Here is the source I've written:

#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;
}

I've included the entire program to give you an idea of where I plan to go with the coding. Thanks in advance for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独留℉清风醉 2024-10-08 00:53:19

你好,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 的地址开始,遍历数组的所有位 - size id 指定为 sizeof tmp - 并将这些位设置为指定的字符 - 在本例中为 NULL。这样我们就不必在每次读取 C 字符串时记住附加 NULL 字符;前提是 tmp 的大小足够大。方便的!

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

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

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

希望这有帮助。

干杯!
快乐学习。

Hello C++ new programmer! Welcome to the awesomeness of coding in C/C++.

I know you just started C++. But to fix your problem, we have to touch a bit of C. C++ so happens to be a superset of C. Meaning everything that can be done in C will work in a C++ program.

Ok enough words, code time. Replace the code you use to get input from your input file with this:

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

Let's walk through it.

char tmp[256]; will create a temporary array for reading in the values. The size of this array may vary with the average length of names you receive.

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.

∝单色的世界 2024-10-08 00:53:19

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

使用 getline 将完整行获取到名为 linestring 中。

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

line.substr 将名称提取为新的 string 直到刚好的位置find

将星号之外的另一个 substr 放入名为 remainderstringstream 中。

使用operator>>直接从中读取intdoubleremainder>>>小时>>率;

I would not use getline in this way, because you have two separate separator chars to deal with - space and asterisk. Here's what I would do instead:

Use getline to get the FULL line into a string called line.

Find the asterisk using line.find('*');

Extract the name as a new string using line.substr up to the position just found

Take another substr beyond the asterisk into a stringstream called remainder.

Read the int and double directly from it using operator>>: remainder >> hours >> rate;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文