Write a c++ program (Binary Random Access Database) using a class that has array
ID: 3704933 • Letter: W
Question
Write a c++ program (Binary Random Access Database) using a class that has array data members name, salary, year_hired. This program will
1) write the data for 10 records to a random access file
2) allow the user to output the name, salary, hire_date for a selected record
3) allow the user to change the name, salary, or hire_date for a selected record.
4) allow the user to add a record.
Note: The record number is the subscript of the array for name, salary, year_hired.
Must have ios::binary in the program
C++ PROGRAM MUST COMPILE AND MUST WRITE TO AND READ FROM A RANDOM ACCESS (BINARY) FILE
Thank You!
Explanation / Answer
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void GetList(ifstream& fileIn, int hiredate[], int employee[], int salary[], int totalRecs);
void menuaction(int hiredate[], int employee[], int salary[], int totalRecs);
void DisplayList(int hiredate[], int employee[], int salary[], int totalRecs);
void sortarray(int hiredate[], int employee[], int salary[], int totalRecs);
void showarray(int hiredate[], int employee[], int salary[], int totalRec);
void UnOrdInsert(int hiredate[], int employee[], int salary[], int totalRecs);
const int MAX_LIST_SIZE = 25;
int main()
{
// Array of list elements
int hiredate[MAX_LIST_SIZE];
int employee[MAX_LIST_SIZE];
int salary[MAX_LIST_SIZE];
int totalRecs = 0;
// Set up file
ifstream fileIn;
fileIn.open("employeedata.txt"); //Open The File
if (fileIn.fail()) // Test for file existence
{
cout << "Problem opening file";
exit(-1);
}
ifstream theData;
GetList(fileIn, hiredate, employee, salary, totalRecs);
menuaction(hiredate, employee, salary, totalRecs);
}
// This function reads integers from a file and stores the values in
// an array. It returns the loaded array and the number of elements
// in the array
void GetList(ifstream& InFile, int hiredate[], int employee[], int salary[], int totalRecs)
{
int i = 0;
// Priming read
InFile >> hiredate[i] >> employee[i] >> salary[i];
while (!InFile.eof())
{
i++; // Add one to pointer
// continuation reads
InFile >> hiredate[i] >> employee[i] >> salary[i];
}
totalRecs = i;
}
// This Fuction is the Menu Action
void menuaction(int hiredate[], int employee[], int salary[], int totalRecs)
{
int choice;
do
{
cout << " Employee Data Menu ";
cout << "1. List by hiredate ";
cout << "2. List by employee number ";
cout << "3. Add employee ";
cout << "4. TERMINATE SESSION ";
cout << "Enter your choice: ";
cin >> choice;
if (choice >= 1 && choice <= 3)
{
switch (choice)
{
case 1: DisplayList(hiredate, employee, salary, totalRecs);
break;
case 2: sortarray(hiredate, employee, salary, totalRecs);
showarray(hiredate, employee, salary, totalRecs);
break;
case 3: UnOrdInsert(hiredate, employee, salary, totalRecs);
DisplayList(hiredate, employee, salary, totalRecs);
break;
}
}
else if (choice != 4)
{
cout << "The valid choices are 1 through 4. ";
cout << "Please try again. ";
}
} while (choice != 4);
}
//This function writes a list to console output
void DisplayList(int hiredate[], int employee[], int salary[], int totalRecs)
{
for (int i = 0; i < totalRecs; i++)
cout << hiredate[i] << " " << employee[i] << " " << salary[i] << " " << endl;
cout << endl;
}
// This functions Lists the employees by number using sorting
void sortarray(int hiredate[], int employee[], int salary[], int totalRecs)
{
int temp, temp2, temp3, end;
for (end = totalRecs - 1; end >= 0; end--)
{
for (int i = 0; i < end; i++)
{
if (employee[i] > employee[i + 1])
{
temp = employee[i];
temp2 = hiredate[i];
temp3 = salary[i];
employee[i] = employee[i + 1];
hiredate[i] = hiredate[i + 1];
salary[i] = salary[i + 1];
employee[i + 1] = temp;
hiredate[i + 1] = temp2;
salary[i + 1] = temp3;
}
}
}
}
void showarray(int hiredate[], int employee[], int salary[], int totalRecs)
{
for (int i = 0; i < totalRecs; i++)
cout << hiredate[i] << " " << employee[i] << " " << salary[i] << endl;
}
// This function receives an integer, an array containing
// an unordered list, and the size of the list. It inserts
// a new integer item at the end of the list
void UnOrdInsert(int hiredate[], int employee[], int salary[], int totalRecs)
{
int newint, newint2, newint3;
totalRecs++; // Increment size of list
cout << "Insert New Employee HIREDATE:" << endl;
cin >> newint;
hiredate[totalRecs] = newint;
cout << "Insert New Employee ID NUMBER:" << endl;
cin >> newint2;
employee[totalRecs] = newint2;
cout << "Insert New Employee's Salary:" << endl;
cin >> newint3;
salary[totalRecs] = newint3;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.