Create a menu-driven program that accesses and updates an external text file. Do
ID: 3694675 • Letter: C
Question
Create a menu-driven program that accesses and updates an external text file. Do not perform any calculations.
Employee Data Text:
Bill Carter 333 45.0 52.60 1
SEQUENTIAL EXTERNAL FILE
File Name: employee_data.txt
File Data: Employee Name, ID Number, Hours Worked, Pay Rate, Union Code
File Content:
Bill Carter 333 45.0 52.60 1
Malik Abdul 111 20.0 27.55 3
Mike Gomez 444 51.5 18.05 2
Omar Houng 777 40.5 30.52 1
MENU OPTIONS
Menu options 2 and 4 include SEARCHING an array.
Menu option 4 includes SORTING an array.
When the program starts, display the following menu:
Payroll Maintenance Program
Main Menu
Add a Record
Display a Record
Display the File
Modify a Record
Display Summary Information
Exit Program
Enter your selection (1–6) à
Ensure that the user enters a valid menu option selection (1-6).
Perform the following tasks based on the user’s selection:
Add a Record Option
For record that is to be added, prompt user to enter the employee’s full name (maximum of 25 characters) as a single string data type (e.g., John Doe), a three-digit employee identification number (int), number of hours worked (double), pay rate (double) and union code (int).
Use while loop to verify reasonability of input values. If invalid input, display appropriate error message and request re-entry.
Validate hours worked, pay rate and union code as follows:
Hours worked (0.0 - 60.0)
pay rate (7.50 - 45.00)
union code (1, 2, or 3).
After verifying input data, open the employee_data.txt file
Write the new payroll record to the end of employee_data.txt file.
Close the employee_data.txt file
Display message that record has been added to employee_data.txt file.
After user has viewed message, do not clear the screen.
Display Main menu.
Display a Record Option
Read payroll data from employee_data.txt file into array(s)
Get employee last name from user
Use employee last name to search an array for a record with a matching name
If a matching last name is found,
display the record
close the employee_data.txt file
after user has viewed message, do not clear the screen
display Main menu.
If no matching last name is found,
display an appropriate message
close the employee_data.txt file
after user has viewed message, do not clear the screen
display Main menu.
Display the File Option
Read and display each payroll record from the employee_data.txt file
Close the employee_data.txt file
Do not clear the screen.
Display Main menu.
Modify a Record Option
Read payroll data from the employee_data.txt into an array
Get employee’s last name from user
Use last name to search an array for a matching record
If a matching last name is found,
determine which input data item is to be modified
(changed) – do not change last name
display the record before it is changed
in the array, change the appropriate field (first name, identification number, hours worked, pay rate, or union code) – do not change last name
from array write all payroll data to the employee_data.txt file
display the unsorted employee records from the array
sort the records in the array into ascending sequence by last name
display the sorted employee records from the array
close the employee_data.txt file
after user has viewed message, do not clear the screen
display Main menu.
If no matching last name,
display an appropriate message
after user has viewed message, do not clear the screen
display Main menu.
Display Summary Information
Read and display each payroll record from the employee_data.txt file
Display the last name, ID number and pay rate of the employee who has the highest pay rate.
Display the last name, ID number and pay rate of the employee who has the lowest pay rate.
Close the employee_data.txt file
Do not clear the screen.
Display Main menu.
Exit Program Option
Display an appropriate message
Terminate Program
Explanation / Answer
#include<stdio.h>
#include<fstream>
#include<iostream>
using namespace std;
//creating structure to store file data
struct employee
{
string first;
string last;
int id;
float hrs;
float pay;
int code;
};
int main()
{
//initializing variables
int ch,i=0,id,size,max,min;
float hrs,rate;
char c = 'y';
string name,line,x,y;
//creating array of structures to store file data
struct employee e[10];
fstream fs;
//opening file in read,write and append mode
fs.open ("test.txt", std::fstream::in | std::fstream::out | std::fstream::app);
//storing file data into array of structures
while(getline(fs,line))
{
e[i].first = getline(fs,line, ' ');
e[i].last = getline(fs,line, ' ');
e[i].id = getline(fs,line, ' ');
e[i].hrs = getline(fs,line, ' ');
e[i].pay = getline(fs,line, ' ');
e[i].code = getline(fs,line, ' ');
i++;
}
size = i-1;
do
{ //display menu
cout << "Payroll Maintenance Program Main Menu" << endl;
cout << "Add a Record " << endl;
cout << "Display a Record" << endl;
cout << "Display the File " << endl;
cout << "Modify a Record " << endl;
cout << "Display Summary Information " << endl;
cout << "Exit Program" << endl;
cout << "Enter your selection (1-6)" << endl;
scanf("%d",&ch);
switch(ch)
{ //switch the choice
case 1: cout << "enter first and last name " << endl;//get record values from user
cin >> name;
cout<<"enter id,hrs,rate "<<endl;
cin >> id >> hrs >> rate;
while(hrs>60.0 || rate<7.50 || rate>45.00 || code<1 }} code>3)
{
cout << "enter first and last name " << endl;
cin >> name;
cout<<"enter id,hrs,rate "<<endl;
cin >> id >> hrs >> rate;
}
//insert values into file
fs << name << " " << id << " " << hrs < " " << rate;
size = size+1;
istringstream iss(name);
//update the structure with new array
e[size-1].first = getline(iss,name, ' ');
e[size-1].last = getline(iss,name,' ');
e[size-1].id = id;
e[size-1].hrs = hrs;
e[size-1].pay = rate;
cout << "record has been added " << endl;
break;
case 2: cout << "Enter last name" << endl; //input last name from user
cin >> name;
for(i=0;i<size;i++)
{ //if last name matches with any record,display record
if(name.compare(e[i].last)==0)
{
cout << e[i].first << " " << e[i].last << " " << e[i].id << " " << e[i].hrs << " " << e[i].pay << endl;
}
}
break;
case 3: while(getline(fs,line))//display entire file
cout << line << endl;
break;
case 4: cout << "enter last name" << endl;//input last name from user
for(i=0;i<size;i++)
{
if(name.compare(e[i].last)==0)
{ //if last name matches with any record,modify record
cout << "enter first name " << endl;
cin >> e[i].first;
cout<<"enter id,hrs,rate "<<endl;
cin >> e[i].id >> e[i].hrs >> e[i].rate;
fs << e[i].first << " " << e[i].last << " " << e[i].id << " " << e[i].hrs < " " << e[i].pay;
}
}
break;
case 5: max = e[0].pay;
min = e[0].pay;
for(i=1;i<size;i++)//find maximum pay index
{
if(e[i].pay>max)
max = i;
}
for(i=1;i<size;i++)//find minimum pay index
{
if(e[i].pay<min)
min = i;
}
cout << "max pay rate " << endl;
cout << e[max].first << " " << e[max].last << " " << e[max].id << " " << e[max].hrs < " " << e[max].pay;
cout << "min pay rate " << endl;
cout << e[min].first << " " << e[min].last << " " << e[min].id << " " << e[min].hrs < " " << e[min].pay;
break;
case 6: cout << "exit program " << endl;
exit(0);
break;
default: printf("enter valid selection, between 0 and 6 ");
break;
}
printf("display menu? yes = y and no = n ");
scanf("%c",&c);
}while(c == 'y');
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.