Hey everyone. Before i start i'm writing up this code for my mothers employee database. I have attached two files. An example of how it's going to be run through a picture and the employee database which is a .txt file containing data such as first name, last name etc. I have started the code, and being new to this i came across a problem. I'm having trouble opening the employee.txt file to display like the example in the picture attached. I'm using a switch to call the function but had no luck. Atm i have the menu set up and i really just want to get the "alphabetized listing" working. I was told to also sort it alphabetically, but even if i can get the .txt to open in my program is more than enough help, i can work around it from there. Thanks.

Here's my code.

#include <iostream>
#include <string>
#include <iomanip>
#include <stdlib.h>
#include <fstream>
using namespace std;

// STRUCT
struct HRindex
{

	int dob_y, dob_m, dob_d;
	int start_y, start_m, start_d;
	int pay_rate;
	int exemptions;
	string first_name, last_name, ssn;
	int item_code;

};

ofstream	outputFile;
bool		outputFileIsEmpty = true;
const char*		fileName = "employee.txt";

// FUNCTION PROTOTYPE
void menu();
void readAllOfFile ( ifstream & );
void displayRec ( HRindex & );
void searchRec ( ifstream & );
HRindex readFile ( ifstream &file );



void menu ()
{
	int choice=5;
	ifstream inputFile;
	while (choice !=0)
	{
	cout <<endl;
	cout <<endl;
	cout <<"This program reads a data file of employee data, displays a menu of "<<endl;
	cout <<"choices, and displays the report selected."<<endl;
	cout <<endl;
	cout <<endl;
	cout <<"	------------------------------------ "<<endl;
	cout <<"			MENU				"<<endl;
	cout <<"	==================================="<<endl;
	cout <<endl;
	cout <<"	1. Alphabetizd Listing "<<endl;
	cout <<endl;
	cout <<"	2. Show the record for an employee "<<endl;
	cout <<endl;
	cout <<"	3. Budget for employee pay increases "<<endl;
	cout <<endl;
	cout <<"	4. Retirement elegibility "<<endl;
	cout <<endl;
	cout <<"	0. Exit "<<endl;
	cout <<endl;
	cout <<endl;
	cout <<"Enter your choice  := ";
	cin >>choice;
	switch (choice)
	{
		case 1: 
			// call function to open and sort employee.txt?
			//readAllOfFile ( ifstream & );


			
			break;
		case 2:
			
			break;
		case 3:
			cout <<endl;
			cout <<endl;
			cout <<"hello 2 "<<endl;
			break;
		case 4:
			cout <<endl;
			cout <<endl;
			cout <<"hello 3 "<<endl;
			break;
		


		default :
			// pops up for wrong user selection
			system("cls");
			
	}
	}

	cin.get();
	}




void readAllOfFile( ifstream &file )
{
	cout <<endl;
	cout <<endl;
	// displays the  report heading 
	cout << "Employee    "<<"SSN   "<<"DOB   " <<"Start Date    "<<"Pay Rate	"<<"Exemptions	"<<endl;
	cout <<endl;
	cout << "====================================================================================="<<endl;
	cout <<endl;
	while ( !file.eof() )
		{
			// calls the readfile functnio under the display function 
			displayRec( readFile( file ) );
			cout << endl;
		}
	return;
}

HRindex readFile ( ifstream &file )
{
	HRindex newHRindex;
	
	file >> newHRindex.first_name;
	file >> newHRindex.last_name;
	file >> newHRindex.ssn;
	file >> newHRindex.dob_y;
	file >> newHRindex.dob_m;
	file >> newHRindex.dob_d;
	file >> newHRindex.start_y;
	file >> newHRindex.start_m;
	file >> newHRindex.start_d;
	file >> newHRindex.pay_rate;
	file >> newHRindex.exemptions;
	return newHRindex;
}

void displayRec ( HRindex &newHRindex )
{
	cout <<endl;
	cout <<newHRindex.first_name<<"		"<<newHRindex.last_name <<"		"<< newHRindex.ssn <<"		" <<endl;//<<newHRindex.dob_y <<"		"<<newHRindex.dob_m <<"		"<<newHRindex.dob_d<<"		"<<newHRindex.start_y<<"		"<<newHRindex.start_m<<"		"<<newHRindex.start_d<"		"<<newHRindex.pay_rate<"		"<<newHRindex.exemptions <<endl;
	return;
}



//----------------------------------------------
// main menu
int main ()
{
	// calls the menu function 
	menu();
	cin.get();
	return 0;
}

Your program never opens the input file. Where's the infile.open() statement?

The while statement in ReadAllOfFile() is incorrect. The eof() doesn't work that way. Here is how it should be coded. You should change readFile() to return the reference to ifstream instead of the structure and pass the structure by reference. That allows ReadAllOfFile() to detect the end of the file

ifstream& readFile ( ifstream &file, HRIndex& newHRIndex )
{
    // if end-of-file do nothing
    if( file >> newHRindex.first_name ) 
    {
	file >> newHRindex.last_name;
	file >> newHRindex.ssn;
	file >> newHRindex.dob_y;
	file >> newHRindex.dob_m;
	file >> newHRindex.dob_d;
	file >> newHRindex.start_y;
	file >> newHRindex.start_m;
	file >> newHRindex.start_d;
	file >> newHRindex.pay_rate;
	file >> newHRindex.exemptions;
    }
    return file
}

...
...
    HRIndex newHRFile;
    while ( ReadFile(file, newHRFile ))
    {
        // calls the readfile functnio under the display function 
        displayRec( newHRFile );
        cout << endl;
    }		}

If you have to sort that file you will want to read the rows into a vector instead of printing them out,
like this

vector<HRIndex> array;
    HRIndex newHRFile;
    while ( ReadFile(file, newHRFile ))
    {
        array.push_back(newHRFile );
    }		
}

now you can sort the vector then print is out if you want to.

In order to input the data, you must open the file first. Then you need to read each record and load your structure. Maybe you should read the entire file when you start the program and just keep all the records in memory so you have access to them at all times.

Also, you need to read this to fix a bug.

One of the efficient way of sorting is using a multi map. Use the field on which to sort as the key. Fill the multi-map and retrieve it. Your data will be sorted

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.