C++/Follow up
Expert: Joseph Moore - 7/15/2009
QuestionHi there in our previous ? you did give me direction and i did try to compile but when it come to pointers and stings am not full conversant with them because as i had told you earlier am new to programming am just trying to understand every entry you make.
so the ? was this one that i needed you to assist me in getting the codes.
A. Given the following input file called personnel.txt consisting of four columns, employee name, PIN, rate per hour and number of hours worked
Kamau A1002342Q 77.32 37
Njoroge A2131155Q 88.32 40
Cheprotich B3232415W 96.54 40
Taabu A4344563W 90.80 35
Write a C++ program that will do the following:
a) Read data from the input file above located in folder
c:companypayroll.
b) Calculate the net pay due for each employee
c) print the output so that it is organized as follows
PIN Name Netpay ----------------------------------------------------------
A1002342Q Kamau 2860.84
A2131155Q Njoroge 3532.80
B3232415W Cheprotich 3861.60
A4344563W Taabu 3178.00
AnswerOK, here's an implementation of this. Look it over. I'm sure you'll have some questions about some of it. Please, if there is anything that you don't fully understand in the following code, just ask. I'm happy to explain it.
BEGIN CODE:
-------------------------------------------
#include <string>
using std::string;
#include <vector>
using std::vector;
int countLines(FILE* _pFile, int* _pLongestLine);
class employee
{
public:
employee()
{
mPIN = "0";
mName = "Invalid";
mRate = 0.f;
mHours = 0;
}
void setPIN(const char* _szPIN)
{
mPIN = _szPIN;
}
const string& getPIN()
{
return mPIN;
}
void setName(const char* _szName)
{
mName = _szName;
}
const string& getName()
{
return mName;
}
void setRate(float _rate)
{
mRate = _rate;
}
float getRate()
{
return mRate;
}
void setHours(int _hours)
{
mHours = _hours;
}
int getHours()
{
return mHours;
}
private:
string mPIN;
string mName;
float mRate;
int mHours;
};
employee* createEmployeeFromLine(char* _szLine)
{
employee* retVal = new employee;
//Kamau A1002342Q 77.32 37
char delims[] = " \t";
char* szOut = strtok(_szLine, delims);
if (szOut)
{
retVal->setName(szOut);
}
else
{
delete retVal;
return NULL;
}
szOut = strtok(NULL, delims);
if (szOut)
{
retVal->setPIN(szOut);
}
else
{
delete retVal;
return NULL;
}
szOut = strtok(NULL, delims);
if (szOut)
{
retVal->setRate((float)atof(szOut));
}
else
{
delete retVal;
return NULL;
}
szOut = strtok(NULL, delims);
if (szOut)
{
retVal->setHours(atoi(szOut));
}
else
{
delete retVal;
return NULL;
}
return retVal;
}
int main()
{
FILE* pFile = fopen("personnel.txt", "rt");
int nLongestLine;
int nLines = countLines(pFile, &nLongestLine);
char* szLineStorage = new char[nLongestLine + 1];
vector<employee*> employeeVec;
while (fgets(szLineStorage, nLongestLine + 1, pFile))
{
employee* pEmp = createEmployeeFromLine(szLineStorage);
if (pEmp)
employeeVec.push_back(pEmp);
}
printf("PIN\t\tName\t\tPay\n");
printf("----------------------------------------\n");
for (int i = 0; i < (int)employeeVec.size(); ++i)
{
printf("%s\t%s\t%.2f\n", employeeVec[i]->getPIN().c_str(), employeeVec[i]->getName().c_str(), employeeVec[i]->getRate() * employeeVec[i]->getHours());
}
}
// This is a utility function that counts the number of lines in a file as well as
// the length of the longest line.
int countLines(FILE* _pFile, int* _pLongestLine)
{
// Storage for each character in the file, with previous initialized to \n
// to prevent empty files from ruining this algorithm.
char currChar;
char prevChar = '\n';
// Number of lines
int numLines = 0;
// Tracks the current and longest line lengths
int curLineLength = 0;
int longestLine = 0;
// Keeps file position kosher
long curFilePos = 0;
// Make sure the user gave us a file
if (!_pFile)
return 0;
// Store the current file position
curFilePos = ftell(_pFile);
// Set the file pointer to the beginning of the file
fseek(_pFile, 0, SEEK_SET);
// Go through every character in the file.
while ((currChar = fgetc(_pFile)) != EOF)
{
// Find newline characters
if (currChar == '\n')
{
// Increment number of lines.
++numLines;
// Test for a new longest line
if (curLineLength > longestLine)
longestLine = curLineLength;
// Reset current line length
curLineLength = 0;
}
// Keep a copy to later test whether...
prevChar = currChar;
++curLineLength;
}
// Test for a new longest line
if (curLineLength > longestLine)
longestLine = curLineLength;
// If the last line did not end in a newline, then we need to add one more
// line to the total.
if (prevChar != '\n')
{
++numLines;
}
// Return to the starting file position.
fseek(_pFile, curFilePos, SEEK_SET);
// If the user wants it, store the longest line length
if (_pLongestLine)
*_pLongestLine = longestLine;
// Return the counted number of lines
return numLines;
}
-------------------------------------------
END CODE