Hi all. I am trying to create a program to take in records of a companies payroll. I get the data then write it to a binary file. When I want to delete a record I ask for the specific timesheets name and the employee ID then read the file into a struct. I am having a problem removing a record and then bumping everything up a row. It's specifically with the two char arrays in my struct (I have put a comment next to the problem so you can find it quicker). Any help with this would be appreciated. Thanks!

#include <iostream>
#include <fstream>
#include <string.h>
#include <windows.h>

using namespace std;

struct times{
	int empID;
	char lastName[15];
	char firstName[15];
	int dept;
	double hours;
	double rate;
};

times timesheet[200];
times timesheet2[200];
string filename;
string filename2;
int rows;
int selection;
int size;

void MainMenu();
void readFileIntoArrayByRecord();
void getTimeData();
void writeBinary();
void deleteRecord();

int main()
{
MainMenu();
}

void MainMenu()
{
system("CLS");
cout << "                                     Main Menu" << endl<< endl;
cout << "                        1: Add Timesheet Data" << endl;
cout << "                        2: Delete a Record"<< endl;
cout << "                        3: Produce Wage Slips"<< endl;
cout << "                        4: Tax Calculator"<< endl;
cout << "                        5: Produce Department Pay Report"<< endl;
cout << "                        6: Produce Employee Pay Report"<< endl;
cout << "                        7: Exit"<< endl<<endl;
cout << "Please enter your selection: ";
cin >> selection;
cout << endl;
if (selection == 1){
getTimeData();
}
if (selection == 2){
deleteRecord();
}
else if (selection == 7){
exit(1);
}
else
cout << "Invalid selection"<< endl;
system("PAUSE");
}

void getTimeData()
{
    int enterNext;
    system("CLS");
    cout << "Enter the end of week date (dd.mm.yy: include the full stops)" << endl;
    cout << "This will be used for all data on this timesheet" << endl;
    cin >> filename;
filename = filename + ".txt";

    for (int i = 0; i <= 200; i++){
    cout << "Enter employee ID: " << endl;
	cin >> timesheet[i].empID;
	cout << "Enter last name: " << endl;
	cin >> timesheet[i].lastName;
	cout << "Enter first name: " << endl;
	cin >> timesheet[i].firstName;
	cout << "Enter department no.: " << endl;
	cin >> timesheet[i].dept;
	cout << "Enter hours worked: " << endl;
	cin >> timesheet[i].hours;
	cout << "Enter hourly rate: " << endl;
	cin >> timesheet[i].rate;
	rows++;
writeBinary();

system("CLS");

cout << "1: Enter another record"<< endl;
cout<<"2: Return to Main Menu"<<endl;
cout<<"3: Exit program"<<endl;
cin >> enterNext;
if (enterNext == 2){
MainMenu();
}
else if (enterNext == 3){
exit(1);
}
}
}

void deleteRecord(){
    int removeID;
    int i;
    cout << "Enter the timesheet date you want to edit: ";
    cin >> filename2;
    filename2 = filename2 + ".txt";
    cout << endl << "Enter the employee ID you wish to remove: ";
    cin >> removeID;
    readFileIntoArrayByRecord();

for (i=0;i<size;i++)
{
    if (timesheet2[i].empID == removeID) 
    {
        for (int j=i;j<size-1;j++)
        {
            timesheet2[j].empID = timesheet2[j+1].empID;
            timesheet2[j].lastName = timesheet2[j+1].lastName; //problem is here
            timesheet2[j].firstName = timesheet2[j+1].firstName; //and here
            timesheet2[j].dept = timesheet2[j+1].dept;
            timesheet2[j].hours = timesheet2[j+1].hours;
            timesheet2[j].rate = timesheet2[j+1].rate;
        }
        size--;
        break; 
    }
}
}

void writeBinary()
{
    ofstream myfile(filename.c_str(),ios::binary | ios::app);
	if (myfile.is_open())
	{
		myfile.write((char *)&timesheet, sizeof(times) * rows);
		myfile.close();
	}
	else
	  cout << "Unable to open file";
}

void readFileIntoArrayByRecord()
{
	rows=0;
	ifstream myfile(filename2.c_str(),ios::binary);
	if (!myfile.is_open())
		cout << "Unable to open file: " << filename.c_str() << endl;
	else
	{
		while ( !myfile.eof() )
		{
			myfile.read((char *)&timesheet2[rows], sizeof(times));
			rows++;
			size++;
		}
		myfile.close();
	}
}

There's no need to copy members individually. You can simply copy the structure objects:

timesheet[j] = timesheet[j + 1];

Ah, I always trip up on simple things! Thanks very much!

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.