开发者

Reading input from text file to array in c++ [duplicate]

开发者 https://www.devze.com 2023-01-23 12:21 出处:网络
This question already has answers here: Reading input from text file to array in c++ (2 answers) Closed 4 years ago.
This question already has answers here: Reading input from text file to array in c++ (2 answers) Closed 4 years ago.

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开发者_如何学Python.
 {
  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!


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.


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;

0

精彩评论

暂无评论...
验证码 换一张
取 消