C++ Program Using Records (structs), Arrays, filestream variables, functional de
ID: 3574936 • Letter: C
Question
C++ Program Using Records (structs), Arrays, filestream variables, functional decomposition, and functions and parameter passing.
Create a C++ program with the following requirements. You will manage employee database. There will be a maximum of 25 employees. The program will get its input from 3 separate input files (using filestreams)and write its output to the default output source.
REQUIRED DATA STRUCTURE
-Create an array of records (structs), the minimum fields in the record should be
a. first name and last name (string)
b. ID # (int)
c. job status (char) F=full-time P=part-time
d. hourly pay rate (double)
e. hours worked (double)
INPUT FILE DESCRIPTIONS
a. The first input file will consist of several lines of data representing employee information. Each line will be formatted as follows:
b. firstname lastname ID# jobstatus payrate
c. Names may need to be reformatted (first letter capitalized, remaining lower case). All other values will be present and valid in this file.
d. The second input file will consist of a series of changes to be made to the employee database, one per line. Each line will start with an integer that is supposed to represent an ID#. If the ID# is valid (matches an employee), then a one letter command (that represents the field to be changed) and a new value for that field will follow. If the ID# is invalid, the remainder of the line should be ignored. Maximum of 80 characters on a line, use ignore function, ifstvar.ignore(80,' ') or you can write your own function to discard the remainder of the current line. Note: the ID#s will not be in the same order as they were in the first input file and there may be more than one change for an employee.
Command Codes
S = job status P = pay rate
Sample data sets:
56 P 10.50
29 S F
Note that the name and ID# fields will not be changed.
e. The third data file will consist of several data sets that indicate the hours worked by employees during the current pay period. Each line will start with an integer that is supposed to represent an ID#. If the ID# is valid (matches an employee), then it will be followed by a floating point value that represents the number of hours worked by the employee. If the ID# is invalid, the remainder of the line should be ignored (maximum of 80 characters). Note: the ID#s will not be in the same order as they were in the first input file and there may be more than one data set for an employee.
Sample data sets:
56 8.0
29 3.5
Processing
Your program should:
a. interactively prompt the user for the name of the first input file, open the file, and read the data from the file, storing the employee information into your array and counting the number of employees
b. sort the array of employees into alphabetical order (by last name, you may assume last names will be unique)
c. generate an alphabetized report that lists all of the employees by name with their ID#, job status, and pay rate (see sample below) BEFORE the second input file is read
d. interactively prompt the user for the name of the second input file, open the file
e. read and process the change commands, making the specified changes to the employee information in your array, for each change that is made display a message indicating the ID# and change made, if an ID# is invalid display a message that states the bad ID# and indicates that it could not be found
e. generate an alphabetized report that lists all of the employees by name with their ID#, job status, and pay rate (see sample below) BEFORE the third input file is read
f. interactively prompt the user for the name of the third input file, open the file
g. read and process the hours worked data, determining the total number of hours worked by each employee, no output is required when processing this data, note that there may be several data sets that pertain to the same employee
h. generate an alphabetized report that lists all of the employees by name with their pay rate, hours worked, and total pay due; the last line of the report should display the total hours worked and total amount due to employees; do not total any other columns (see sample below)
i. Make sure that all output is nicely formatted (as in examples) and leave blank lines between reports to enhance readability.
Calculation of Pay
To calculate pay, part-time employees are paid their regular hourly rate for each hour worked. Full-time employees earn overtime pay (1.5 times their regular hourly pay) for time worked over 40 hours.
Formatting Specifications
a. First and last names will be strings with a maximum length of 10 characters each.
b. ID#'s will be integers with a maximum length of 3 digits.
c. All commands will be capital letters.
d. Pay rate and hours worked will be double values. Maximum value for each is 100.00.
e. Display all floating point values with 2 digits to right of decimal.
f. Include dollar signs ($) for all dollar amounts.
NOTES and OTHER REQUIREMENTS:
FUNCTIONS MUST BE USED TO MODULARIZE THE PROGRAM. At least 5 functions (besides main) must be implemented. The functions must be meaningful.
Assumptions about input:
the data files will exist and will not be empty
each input value will be separated by whitespace (blanks and/or linefeeds)
the last line in each data file will be terminated with a linefeed (' ')
Program must be designed to interactively prompt the user for the name of each input file in the order described above. Filestream variable(s) must be used to represent files.
All output will be written to the screen.
Include header files for all library functions used.
Each input file can only be read one time.
Sample terminal session:
[keys]$ more employees
max martin 123 F 20.00
Michael Malloy 56 P 7.50
maRy MiLLs 555 F 17.50
mAUry miLsAp 89 F 6.15
[keys]$ more changes
555 P 18.25
11 nonsense to be ignored
56 S F
555 S P
[keys]$ more hrsworked
123 7.5
56 6.0
99 invalid id#
555 2.5
56 40.0
123 10.0
[keys]$ g++ assign06.cpp
[keys]$ ./a.out
Enter name of first data file
employees
NAME ID# STATUS RATE
Malloy,Michael 56 P $ 7.50
Martin,Max 123 F $ 20.00
Mills,Mary 555 F $ 17.50
Milsap,Maury 89 F $ 6.15
Enter name of second data file
changes
ID# 555 hourly pay rate changed to 18.25
ID# 11 is invalid
ID# 56 status changed to F
ID# 555 status changed to P
NAME ID# STATUS RATE
Malloy,Michael 56 F $ 7.50
Martin,Max 123 F $ 20.00
Mills,Mary 555 P $ 18.25
Milsap,Maury 89 F $ 6.15
Enter name of third data file
hrsworked
NAME PAY RATE HOURS TOTAL
Malloy,Michael $ 7.50 46.00 $ 367.50
Martin,Max $ 20.00 17.50 $ 350.00
Mills,Mary $ 18.25 2.50 $ 45.62
Milsap,Maury $ 6.15 0.00 $ 0.00
TOTAL 66.00 $ 763.12
*DO NOT USE ALGORITHM, VECTORS, TIME, OR SSTREAM TO SOLVE THE PROBLEM. YOU MUST USE STRUCTS TO HELP SOLVE THE PROBLEM.
Please add comments to the program if possible.
Here is what bubblesort is if you don't know it, I might have to use it in the program:
void bubblesort(int list[ ],int count)
// Sort an array of integers into descending order.
// Parameters:
// list: array of integers to be sorted
// count: (integer) number of values in the array
// Value passed back: sorted list
{
int temp; //place holder when values are interchanged
for (int i=0; i < count-1; i++)
for (int j=0; j < count-(i+1); j++)
if (list[j] < list[j+1])
{
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
}
Here is a sample code to first part of the problem, that can be used AS A REFERENCE.
#include <fstream>
#include <string>
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;
const int MAX_E = 2,MAX_HW = 5;
const int MAX = 50;
const int N=25;
const int MAXPTS=450;
struct astudent
{
string first,last; //student's first & last name
int idnum; //student's ID#
int totpts; //total points earned
int exams[MAX_E]; //student's exam scores
int hw[MAX_HW]; //student's homework scores
};
void get_data(astudent[],int&);
void read_scores(ifstream&,int[],int);
void fixstring(string&);
void bubblesort(astudent[],int);
void print_roster(astudent[],int);
int sum_points(astudent);
char assign_grade(int);
int main()
{
astudent theclass[MAX];//array to store student info
int n; //number of students in the class
get_data(theclass,n);
bubblesort(theclass,n);
print_roster(theclass,n);
return 0;
}
void get_data(astudent cs[],int& n)
{
string filename; //name of input file
ifstream in; //input file
cout << "Please enter the name of the input file ";
cin >> filename;
in.open(filename.c_str());
n=0;
in >>cs[n].idnum;
while(in)
{
in >>cs[n].last>>cs[n].first;
cs[n].totpts=0;
fixstring(cs[n].last);
fixstring(cs[n].first);
read_scores(in,cs[n].exams,MAX_E);
read_scores(in,cs[n].hw,MAX_HW);
n++;
in >>cs[n].idnum;
while(in)
{
in >>cs[n].last>>cs[n].first;
cs[n].totpts=0;
fixstring(cs[n].last);
fixstring(cs[n].first);
read_scores(in,cs[n].exams,MAX_E);
read_scores(in,cs[n].hw,MAX_HW);
n++;
in >>cs[n].idnum;
}
in.close();
}
void read_scores(ifstream& in,int list[],int size)
{
for (int i=0; i<size; i++)
in >> list[i];
}
void fixstring(string& word)
{
int wlen=word.length();
word[0] = toupper(word[0]);
for (int i=1; i<wlen; i++)
word[i] = tolower(word[i]);
}
void print_roster(astudent list[],int n)
{
int points;
char grade;
cout << left << setw(N) << "NAME" << right << setw(N-15) << "POINTS"
<< setw(N-15) << "GRADE" << endl;
for (int i=0;i<n;i++)
{
cout << setw(N) << left << list[i].last+','+list[i].first;
points = sum_points(list[i]);
list[i].totpts = points;
grade = assign_grade(points);
cout << right << setw(N-15) << points << setw(N-15) << grade << endl;
}
}
int sum_points(astudent person)
{
int sum=0;
for (int i=0; i<MAX_E; i++)
sum = sum+person.exams[i];
for (int i=0; i<MAX_HW; i++)
sum = sum+person.hw[i];
return sum;
}
char assign_grade(int points)
{
if (points >= 0.9*MAXPTS)
return 'A';
else if (points >= 0.8*MAXPTS)
return 'B';
else if (points >= 0.7*MAXPTS)
return 'C';
else if (points >= .6*MAXPTS)
return 'D';
else
return 'F';
}
void bubblesort(astudent list[],int n)
{
int i; int j;
astudent temp;
for (i=0; i < n-1; i++)
for (j=0; j < n-(i+1); j++)
if (list[j].last > list[j+1].last)
{
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
}
Explanation / Answer
#include<iostream>
#include<string>
#include<vector>
#include<fstream>
#include<algorithm>
using namespace std;
//structure for an employee
typedef struct employee
{
string firstname;
string lastname;
int ID;
char job_status;
double hourly_pay_rate;
double hours_worked;
}employee;
void change_name_syntax(std::vector<employee>& emp)
{
string temp;
for(unsigned int i = 0; i < emp.size(); i++)
{
temp = emp[i].firstname;
temp[0] = std::toupper(temp[0]);
for(std::size_t j = 1; j < temp.length(); j++)
{
temp[j] = std::tolower(temp[j]);
}
// cout<<temp<<endl;
emp[i].firstname = temp;
}
for(unsigned int i = 0; i < emp.size(); i++)
{
temp = emp[i].lastname;
temp[0] = std::toupper(temp[0]);
for(std::size_t j = 1; j < temp.length(); j++)
{
temp[j] = std::tolower(temp[j]);
}
// cout<<temp<<endl;
emp[i].lastname = temp;
}
}
void print_array(std::vector<employee> emp)
{
for(unsigned int i = 0; i < emp.size(); i++)
{
cout<<emp[i].lastname<<","<<emp[i].firstname<<" "<<emp[i].ID<<" "<<emp[i].job_status<<" "<<"$ "<<emp[i].hourly_pay_rate<<endl;
}
}
bool my_cmp(const employee& a, const employee& b)
{
return a.lastname < b.lastname;
}
int find_id(int id, std::vector<employee> emp)
{
for(std::size_t i = 0; i < emp.size(); i++)
{
if(emp[i].ID == id)
return i;
}
return -1;
}
void initialize_hours_worked(std::vector<employee>& emp)
{
for(std::size_t i = 0; i < emp.size(); i++)
emp[i].hours_worked = 0;
}
int main()
{
string input_filename; //for filename
vector<employee> emp; //vector to store employee information
string fn,ln; //for firstname and lastname
int id,index=0;
char js,ch;
double hpr,hw;
cout << "Enter Name of the first data file"<<endl;
cin>>input_filename; //getting first filename
std::ifstream myfile(input_filename.c_str()); //reading filestream into myfile
while(myfile >> fn >> ln >> id >> js >> hpr ) //reading file line by line and store in vector
{
emp.push_back(employee());
emp[index].firstname = fn;
emp[index].lastname = ln;
emp[index].ID = id;
emp[index].job_status = js;
emp[index].hourly_pay_rate = hpr;
index++;
}
change_name_syntax(emp); //function to change the firstname and lastname with given condition i.e. first letter Capital and rest small
std::sort(emp.begin(), emp.end(), my_cmp);
cout<<"NAME ID# STATUS RATE"<<endl;
print_array(emp); //prinitng vector containing all the information
cout << "Enter Name of the Second data file"<<endl;
cin>>input_filename; //reading second file name
std::ifstream myfile1(input_filename.c_str()); //reading filesteam into myfile1
while(myfile1 >> id ) //reading ID
{
int found = find_id(id,emp); //finding ID into vector
if(found == -1) //if ID not found then printing message
{
cout << "ID# "<<id<<" is invalid"<<endl;
myfile1.ignore(80,' '); //ignoring line next to invalid ID
}
else //ID is valid
{
myfile1 >> ch; //reading next S or P
if(ch == 'P')
{
myfile1 >> hpr; //reading next value pay rate
cout << "ID# "<<id<<" hourly pay rate changed to "<<hpr<<endl;
emp[found].hourly_pay_rate = hpr; //setting pay rate of the employee
}
else
{
myfile1 >> js; //reading next value Full time or Part time
cout << "ID# "<<id<<" status changed to "<<js<<endl;
emp[found].job_status = js; //setting value into vector
}
}
}
cout<<" NAME ID# STATUS RATE"<<endl;
print_array(emp); //printing vector
cout << "Enter Name of the third data file"<<endl;
cin>>input_filename; //reading third file
std::ifstream myfile2(input_filename.c_str()); //reading filestream into myfile2
initialize_hours_worked(emp); //initialize hors worked to 0
while(myfile2 >> id ) //reading ID
{
int found = find_id(id,emp);
if(found == -1) //IF ID not found then skipping data
{
myfile2.ignore(80,' ');
}
else //ID matched
{
myfile2 >> hw; //reaidng hours worked
emp[found].hours_worked += hw; //adding the value into vector of particular ID
}
}
//displaying data
cout<<" NAME PAY RATE HOURS TOTAL"<<endl;
double hours_total=0,total=0;
for(size_t i = 0; i < emp.size(); i++)
{
double temp = emp[i].hourly_pay_rate * emp[i].hours_worked;
cout << emp[i].lastname<<","<<emp[i].firstname<<" "<<" $
"<<emp[i].hourly_pay_rate<<" "<<emp[i].hours_worked<<" "<<temp<<endl;
hours_total += emp[i].hours_worked;
total += temp;
}
cout <<"TOTAL "<<hours_total<<" "<<total<<endl;
//closing all files
myfile1.close();
myfile2.close();
myfile.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.