C++ Program Using Records (structs), Arrays, filestream variables, functional de
ID: 3572623 • 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 OR VECTORS 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;
}
}
Explanation / Answer
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<windows.h>
//#include <ctime>
//#include <dos.h>
#include<dos.h>
#include<conio.h>
#include<cstdio>
#define max 25
using namespace std;
struct employee
{
char name[25];
long int code;
char designation[25];
int exp;
int age;
};
int num;
employee emp[max],tempemp[max],sortemp[max],sortemp1[max];
int main()
{
system("cls");
void build();
void list();
void insert();
void deletes();
void edit();
void search();
void sort();
char option;
void menu();
menu();
while((option=cin.get())!='q')
{
switch(option)
{
case 'b':
build();
break;
case 'l':
list();
break;
case 'i':
insert();
break;
case 'd':
deletes();
break;
case 'e':
edit();
break;
case 's':
search();
break;
case 'n':
sort();
break;
}
menu();
}
return 0;
}
void menu()
{
system("cls");
// highvideo();
cout<<" ";
printf(" ***** Employees Management System 1.0 ***** ");
//normvideo();
cout<<endl;
cout<<" ";
cout<<" Press b---->Built The Employee Table ";
cout<<" ";
cout<<" Press l---->List The Employee Table ";
cout<<" ";
cout<<" Press i---->Insert New Entry ";
cout<<" ";
cout<<" Press d---->Delete An Entry ";
cout<<" ";
cout<<" Press e---->Edit An Entry ";
cout<<" ";
cout<<" Press s---->Search Arecord ";
cout<<" ";
cout<<" Press n---->Sort The Table ";
cout<<" ";
cout<<" Press q---------->Quit Program ";
cout<<" ";
cout<<" Select Your Option Please ====> ";
}
void build()
{
system("cls");
// highvideo();
printf("Build The Table");
cout<<endl;
//normvideo();
cout<<"maximum number of entries ----- > 25"<<endl;
cout<<"how many do you want ----->";
cin>>num;
cout<<"Enter The Following Items"<<endl;
for(int i=0;i<=num-1;i++)
{
cout<<" Name ";
cin>>emp[i].name;
cout<<"Code ";
cin>>emp[i].code;
cout<<"Designation ";
cin>>emp[i].designation;
cout<<"Years of Experience ";
cin>>emp[i].exp;
cout<<"Age ";
cin>>emp[i].age;
}
cout<<"going to main menu";
Sleep(500);
}
void list()
{
system("cls");
// highvideo();
printf(" ********List The Table********");
cout<<endl;
//normvideo();
cout<<" Name Code Designation Years(EXP) Age "<<endl;
cout<<" ------------------------------------------------------"<<endl;
for(int i=0;i<=num-1;i++)
{
cout<<setw(13)<<emp[i].name;
cout<<setw(6)<<emp[i].code;
cout<<setw(15)<<emp[i].designation;
cout<<setw(10)<<emp[i].exp;
cout<<setw(15)<<emp[i].age;
cout<<endl;
}
cout<<"going to main menu";
getch();
}
void insert()
{
system("cls");
int i=num;
num+=1;
// highvideo();
printf("Insert New Record");
cout<<endl;
//normvideo();
cout<<"Enter The Following Items"<<endl;
cout<<"Name ";
cin>>emp[i].name;
cout<<"Code ";
cin>>emp[i].code;
cout<<"Designation ";
cin>>emp[i].designation;
cout<<"Years of Experience ";
cin>>emp[i].exp;
cout<<"Age ";
cin>>emp[i].age;
cout<<endl<<endl;
cout<<"going to main menu";
Sleep(500);
}
void deletes()
{
system("cls");
// highvideo();
int code;
int check;
printf("Delete An Entry");
//normvideo();
cout<<endl;
cout<<"Enter An JobCode To Delete That Entry ";
cin>>code;
int i;
for(i=0;i<=num-1;i++)
{
if(emp[i].code==code)
{
check=i;
}
}
for(i=0;i<=num-1;i++)
{
if(i==check)
{
continue;
}
else
{
if(i>check)
{
tempemp[i-1]=emp[i];
}
else
{
tempemp[i]=emp[i];
}
}
}
num--;
for(i=0;i<=num-1;i++)
{
emp[i]=tempemp[i];
}
}
void edit()
{
system("cls");
int jobcode;
// highvideo();
printf(" Edit An Entry ");
cout<<endl;
cout<<endl;
int i;
void editmenu();
void editname(int);
void editcode(int);
void editdes(int);
void editexp(int);
void editage(int);
char option;
//normvideo();
cout<<"Enter An jobcode To Edit An Entry---- ";
cin>>jobcode;
editmenu();
for(i=0;i<=num-1;i++)
{
if(emp[i].code==jobcode)
{
while((option=cin.get())!='q')
{
switch(option)
{
case 'n':
editname(i);
break;
case 'c':
editcode(i);
break;
case 'd':
editdes(i);
break;
case 'e':
editexp(i);
break;
case 'a':
editage(i);
break;
}
editmenu();
}
}
}
}
void editmenu()
{
system("cls");
cout<<" What Do You Want To edit";
cout<<" n--------->Name ";
cout<<" c--------->Code ";
cout<<" d--------->Designation";
cout<<" e--------->Experience ";
cout<<" a--------->Age ";
cout<<" q----->QUIT ";
cout<<" Options Please ---->>> ";
}
void editname(int i)
{
cout<<"Enter New Name-----> ";
cin>>emp[i].name;
}
void editcode(int i)
{
cout<<"Enter New Job Code-----> ";
cin>>emp[i].code;
}
void editdes(int i)
{
cout<<"enter new designation-----> ";
cin>>emp[i].designation;
}
void editexp(int i)
{
cout<<"Enter new Years of Experience";
cin>>emp[i].exp;
}
void editage(int i)
{
cout<<"Enter new Age ";
cin>>emp[i].age;
}
void search()
{
system("cls");
// highvideo();
printf("Welcome To Search Of Employee Database ");
//normvideo();
cout<<endl;
cout<<endl;
int jobcode;
cout<<"You Can Search Only By Jobcode Of An Employee";
cout<<"Enter Code Of An Employee ";
cin>>jobcode;
for(int i=0;i<=num-1;i++)
{
if(emp[i].code==jobcode)
{
cout<<" Name Code Designation Years(EXP) Age ";
cout<<" ------------------------------------------------------ ";
cout<<setw(13)<<emp[i].name;
cout<<setw(6)<<emp[i].code;
cout<<setw(15)<<emp[i].designation;
cout<<setw(10)<<emp[i].exp;
cout<<setw(15)<<emp[i].age;
cout<<endl;
}
}
cout<<"going to main menu";
getch();
}
void sort()
{
system("cls");
// highvideo();
printf("Sort The Databse By JobCode");
//normvideo();
void sortmenu();
void sortname();
void sortcode();
void sortdes();
void sortexp();
char option;
void sortage();
cout<<endl;
cout<<endl;
sortmenu();
while((option=cin.get())!='q')
{
switch(option)
{
case 'n':
sortname();
break;
case 'c':
sortcode();
break;
case 'd':
sortdes();
break;
case 'e':
sortexp();
break;
case 'a':
sortage();
break;
}
sortmenu();
}
}
void sortmenu()
{
system("cls");
cout<<" What Do You Want To edit";
cout<<" n--------->Name ";
cout<<" c--------->Code ";
cout<<" d--------->Designation ";
cout<<" e--------->Experience ";
cout<<" a--------->Age ";
cout<<" q----->QUIT ";
cout<<" Options Please ---->>> "; }
void sortname()
{
system("cls");
int i,j;
struct employee temp[max];
for(i=0;i<=num-1;i++)
{
sortemp1[i]=emp[i];
}
for(i=0;i<=num-1;i++)
{
for(j=0;j<=num-1;j++)
{
if(strcmp(sortemp1[i].name,sortemp1[j].name)<=0)
{
temp[i]=sortemp1[i];
sortemp1[i]=sortemp1[j];
sortemp1[j]=temp[i];
}
}
}
for( i=0;i<=num-1;i++)
{
cout<<" Name Code Designation Years(EXP) Age ";
cout<<" ------------------------------------------------------ ";
for( i=0;i<=num-1;i++)
{
cout<<setw(13)<<sortemp1[i].name;
cout<<setw(6)<<sortemp1[i].code;
cout<<setw(15)<<sortemp1[i].designation;
cout<<setw(10)<<sortemp1[i].exp;
cout<<setw(15)<<sortemp1[i].age;
cout<<endl;
}
cout<<"Press Any Key To Go Back";
getch();
} }
void sortcode()
{
system("cls");
int i,j;
struct employee temp[max];
for(i=0;i<=num-1;i++)
{
sortemp1[i]=emp[i];
}
for(i=0;i<=num-1;i++)
{
for(j=0;j<=num-1;j++)
{
if(sortemp1[i].code<sortemp1[j].code)
{
temp[i]=sortemp1[i];
sortemp1[i]=sortemp1[j];
sortemp1[j]=temp[i];
}
}
}
for( i=0;i<=num-1;i++)
{
cout<<" Name Code Designation Years(EXP) Age ";
cout<<" ------------------------------------------------------ ";
for( i=0;i<=num-1;i++)
{
cout<<setw(13)<<sortemp1[i].name;
cout<<setw(6)<<sortemp1[i].code;
cout<<setw(15)<<sortemp1[i].designation;
cout<<setw(10)<<sortemp1[i].exp;
cout<<setw(15)<<sortemp1[i].age;
cout<<endl;
}
cout<<"Press Any Key To Go Back";
getch();
} }
void sortdes()
{
system("cls");
int i,j;
struct employee temp[max];
for(i=0;i<=num-1;i++)
{
sortemp1[i]=emp[i];
}
for(i=0;i<=num-1;i++)
{
for(j=0;j<=num-1;j++)
{
if(strcmp(sortemp1[i].designation,sortemp1[j].designation)<=0)
{
temp[i]=sortemp1[i];
sortemp1[i]=sortemp1[j];
sortemp1[j]=temp[i];
}
}
}
for( i=0;i<=num-1;i++)
{
cout<<" Name Code Designation Years(EXP) Age";
cout<<" ------------------------------------------------------ ";
for( i=0;i<=num-1;i++)
{
cout<<setw(13)<<sortemp1[i].name;
cout<<setw(6)<<sortemp1[i].code;
cout<<setw(15)<<sortemp1[i].designation;
cout<<setw(10)<<sortemp1[i].exp;
cout<<setw(15)<<sortemp1[i].age;
cout<<endl;
}
cout<<"Press Any Key To Go Back";
getch();
} }
void sortage()
{
system("cls");
int i,j;
struct employee temp[max];
for(i=0;i<=num-1;i++)
{
sortemp1[i]=emp[i];
}
for(i=0;i<=num-1;i++)
{
for(j=0;j<=num-1;j++)
{
if(sortemp1[i].age<sortemp1[j].age)
{
temp[i]=sortemp1[i];
sortemp1[i]=sortemp1[j];
sortemp1[j]=temp[i];
}
}
}
for( i=0;i<=num-1;i++)
{
cout<<" Name Code Designation Years(EXP) Age";
cout<<" ------------------------------------------------------ ";
for( i=0;i<=num-1;i++)
{
cout<<setw(13)<<sortemp1[i].name;
cout<<setw(6)<<sortemp1[i].code;
cout<<setw(15)<<sortemp1[i].designation;
cout<<setw(10)<<sortemp1[i].exp;
cout<<setw(15)<<sortemp1[i].age;
cout<<endl;
}
cout<<"Press Any Key To Go Back";
getch();
} }
void sortexp()
{
system("cls");
int i,j;
struct employee temp[max];
for(i=0;i<=num-1;i++)
{
sortemp1[i]=emp[i];
}
for(i=0;i<=num-1;i++)
{
for(j=0;j<=num-1;j++)
{
if(sortemp1[i].exp<sortemp1[j].exp)
{
temp[i]=sortemp1[i];
sortemp1[i]=sortemp1[j];
sortemp1[j]=temp[i];
}
}
}
for( i=0;i<=num-1;i++)
{
cout<<" Name Code Designation Years(EXP) Age ";
cout<<" ------------------------------------------------------ ";
for( i=0;i<=num-1;i++)
{
cout<<setw(13)<<sortemp1[i].name;
cout<<setw(6)<<sortemp1[i].code;
cout<<setw(15)<<sortemp1[i].designation;
cout<<setw(10)<<sortemp1[i].exp;
cout<<setw(15)<<sortemp1[i].age;
cout<<endl;
}
cout<<"Press Any Key To Go Back";
getch();
} }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.