Hey folks. I am working on a program (really its 2, but they go hand in hand). The first one takes a text file which contains the following:
last name
first name
SSN
Salary
Years employed
There are 7 entries in the text file to be read in...
The program will then read one persons information at a time - storing the information into structures (at least thats what im assuming so far...it works, mostly).
The 2nd part of program is to then take the structure and write it out to a file called employees - sorted through one employee at a time from the text file until a new datafile is created full of these structs.
The 2nd program is to read this new data file called employees and then make an array of a struct and then print out the information for each employee.
For example:
Last: James First: Henry ID: 123456789
Salary: 12000 Years employed: 7
(this will be in a function outside of main)
Then another function that will sort through all the employees in the data file until none are left and print just like above...but for all employees.
My problem is that for whatever reason - the first name is not read into file in the 2nd program (i looked at the employees data file and i see the first persons name - so it SHOULD be read into the 2nd program) all i get is employee 2 to 7. So 6 prints and then an empty 7th like:
Last: First: ID:
Salary: Years Emp:
So obviously the first one isnt being read in correctly.
Here is my first program (which reads from a text file then writes out to another file called employees using the structures it created)
#include <iostream>
#include <fstream>
#include <ostream>
#include <string>
using namespace std;
//Structure
struct Employee
{
char last_name[39];
char first_name[39];
char ss_num[10];
double salary;
int years_employed;
};
//Function Prototypes
//Main function
int main()
{
Employee personInfo;
string end_of_file = "eof";
ifstream inFile;
ofstream outFile;
inFile.open("empinfo.txt");
outFile.open("employees", ios::binary);
if (inFile.fail())
{
cout << "Unable to open empinfo.txt file\n";
exit(1);
}
//Start read of data from file
inFile >> personInfo.last_name;
while (personInfo.last_name != end_of_file)
{
inFile >> personInfo.first_name;
inFile >> personInfo.ss_num;
inFile >> personInfo.salary;
inFile >> personInfo.years_employed;
// Writes out to employees file
outFile.write((char *)&personInfo, sizeof(Employee));
//Restart over from next user in file
inFile >> personInfo.last_name;
}
inFile.close();
outFile.close();
system("PAUSE");
return 0;
}
NOTE : the end_of_file line in the above if is used because there is a line at the end of the text file that says 'eof'. So i am testing for this string, not to see if there nothing left at the end of the text file like you would with eof(). This is why i used this vs the alternative regular way.
My 2nd program looks like:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <ostream>
#include <string>
using namespace std;
//Structure
struct Employee
{
char last_name[39];
char first_name[39];
char ss_num[10];
double salary;
int years_employed;
};
//Function Prototypes
void displayEmployee(Employee);
void displayAllEmployees(Employee [], int);
void sortByName(Employee [], int);
//Main function
int main()
{
Employee personInfo[100];
string end_of_file = "eof";
ifstream inFile;
inFile.open("employees", ios::in | ios::binary);
if (inFile.fail())
{
cout << "Unable to open employees file\n";
exit(1);
}
int i = 0;
inFile.read((char *)&personInfo[i], sizeof(Employee));
while (inFile)
{
inFile.read((char *)&personInfo[i], sizeof(Employee));
i++;
}
inFile.close();
cout << "personInfo[0].lastname should be: " << endl;
cout << personInfo[0].salary << endl;
/*
cout << "Displays ONE persons information!" << endl;
displayEmployee(*personInfo);
cout << "\nDisplays ALL persons information!" << endl;
displayAllEmployees(personInfo, i);
*/
system("PAUSE");
return 0;
}
/****************************************************************************
Function: displayEmployee()
Use: displays one employee from the struct
Arguments: employee struct
Returns: nothing
*****************************************************************************/
void displayEmployee(Employee emp)
{
cout << left;
cout << "Last: " << setw(12) << emp.last_name << "First: " << setw(12)
<< setw(12) << emp.first_name << "ID: " << setw(12) << emp.ss_num
<< "\nSalary: " << setw(12) << emp.salary << "Years Employed: "
<< setw(12) << emp.years_employed << endl;
}
/****************************************************************************
Function: displayAllEmployees()
Use: displays all employees from the struct
Arguments: employee struct , count
Returns: nothing
*****************************************************************************/
void displayAllEmployees(Employee emp[], int count)
{
cout << "\nCOUNT FROM DISPLAY ALL IS: " << count << endl;
for (int i = 0; i < count ; i++)
{
*emp = emp[i];
displayEmployee(*emp);
}
}
If possible - i would like to just figure out why i get this output instead of the correct one...here is the text file i use in program 1.
Kent Robert
123456789
58576.45
7
Jones Mary
111223333
65983.23
12
Adams Joe
111445678
34864.82
5
Zelazneck Jill
333456789
46239.45
7
Anders Lucy
222341234
28564.39
1
Maxwell Lawrence
999887654
100324.65
14
Liu Lee
999887654
54023.44
4
eof
And this is the output of the run of my 2nd program (after i run the 1st one to create the employees data file).
http://www.strifex.net/prog2-run.GIF
I hope someone can help me out for this problem. Thanks once again folks.