我的程序是一个简单的工资单计算器。
如果员工类型为0,则超过40小时的工作时间是一半半。 如果类型为1,他们就不会直接加班加点。
我试图保存struct数组中的所有数据。这是一个家庭作业,我的程序符合要求,但我所遇到的问题是在我为员工输入小时后然后选择菜单选项c,我的输出只是零。
我不确定为什么除了列中的零以外没有输出任何数据。我的编译器没有错误。
我做错了什么?
C ++
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//*********************************************************************************************
// Structure to hold employee ID, Name, Pay rate per hour, Employee type
//*********************************************************************************************
struct employeeRecord {
int IDnum; // holds employee id number
string name; // holds employess name
float payRate; // holds employee pay rate
int empType; // holds employee type
};
//*********************************************************************************************
// structure to hold time sheet
//*********************************************************************************************
struct timeSheet{
float hours; //hours worked
float grossPay; // gross pay
float netPay; // net pay
float taxAmount; // tax amount deduction
};
//*********************************************************************************************
// function prototype
//*********************************************************************************************
void addEmployee(employeeRecord *employee, int &index);
void addTime(timeSheet *time, employeeRecord *employee, int &index);
void calcTime(timeSheet *time, employeeRecord *employee, int &index, float taxRate);
void outTime(timeSheet *time, employeeRecord *employee, int &index);
//*********************************************************************************************
// main function
//*********************************************************************************************
int main(){
float taxRate = .15; // taxrate on pay
char choice; // holds user choice option
int index = 0; // index of array
// create struct arrays to hold data
employeeRecord employee[2];
timeSheet time[2];
// menu title
cout << "\nArmadillo Automotive Group Payroll\n" << endl;
//*********************************************************************************************
// do loop to cycle through menu options & validate input
//*********************************************************************************************
do{
//menu
cout << "\nMenu Options: \n";
cout << "A - Add Employee" << endl;
cout << "B - Add Employee's Hours" << endl;
cout << "C - Print Payroll Report" << endl;
//take user menu choice
cout << "\nEnter Menu Option: ";
cin >> choice;
choice = tolower(choice);
//validate user selection
while (choice != 'a' && choice != 'b' && choice!= 'c'){
cout << "Please enter 'A' 'B' or 'C' : ";
cin >> choice;
}
// Process menu selection choice
if (choice != 'c'){
if (choice == 'a'){
//process new employee
if (index < 2){
addEmployee(employee, index);
}
else{
cout << "\nEmployee index full" << endl;
}
}
// if choice 'b' add employee work hours
else{
addTime(time, employee, index);
}
}
}while (choice != 'c');
// calculates and stores employees time sheet information in struct array
calcTime(time, employee, index, taxRate);
//display pay check outputs
outTime(time, employee, index);
return 0;
}
//*********************************************************************************************
// function to add employee to index
//*********************************************************************************************
void addEmployee(employeeRecord *employee, int &index){
//take employees ID #
cout << "\nEnter employee's ID number: ";
cin >> employee[index].IDnum;
//validate employee id # is greater than 0
while (employee[index].IDnum < 0){
cout << "\nPlease enter an ID # above 0 : ";
cin >> employee[index].IDnum;
}
//take employees name
cout << "\nEnter employee's Name: ";
cin.ignore();
getline(cin, employee[index].name);
//validate line has characters only
//**********************
//**********************
//**********************
//take employees payrate
cout << "\nEnter employee's pay rate: $";
cin >> employee[index].payRate;
//validate payrate is amount above 0
while(employee[index].payRate < 0){
cout << "\nPlease enter a value greater than 0: ";
cin >> employee[index].payRate;
}
// take employees emptype
cout << "\nEnter Employee type \n";
cout << "0 for union, 1 for management: ";
cin >> employee[index].empType;
//validate 0 or 1 was entered
while(employee[index].empType != 0 && employee[index].empType != 1){
cout << "Enter 1 or 0: ";
cin >> employee[index].empType;
}
// increment index for future entry
index++;
}
//*********************************************************************************************
// function to add time to an employee
//*********************************************************************************************
void addTime(timeSheet *time, employeeRecord *employee, int &index){
//set index to zero to start with first employee on list
index = 0;
//main instruction
cout << "Enter timecard information for each employee: " << endl;
//enter hours for each employee for loop
for(int i=0; i < 2; i++){
cout << "Hours worked for " << employee[index].name << ": ";
cin >> time[index].hours;
index++;}
}
//*********************************************************************************************
// function to to calculate timesheet
//*********************************************************************************************
void calcTime(timeSheet *time, employeeRecord *employee, int &index, float taxRate){
index = 0; // set index back to zero to start at first array index
float tempHours; //temp hour hold
float overTime; // overTime hours
float hours = time[index].hours; //hours worked
float grossPay = time[index].grossPay; //employes's gross pay
float netPay = time[index].netPay; //employes's net pay
float taxAmount = time[index].taxAmount; ////employes's tax deduction
int empType = employee[index].empType; // employee type
float payRate = employee[index].payRate; // employes pay rate
for (int i=0; i < 2; i++){
if (empType == 0){
if(hours > 40){
tempHours = 40;
overTime = hours - 40;
grossPay = (overTime * (payRate * 1.5)) + (tempHours * payRate);
taxAmount = grossPay * taxRate;
netPay = grossPay - taxAmount;
}
else{
grossPay = hours * payRate;
taxAmount = grossPay * taxRate;
netPay = grossPay - taxAmount;
}
}
else{
grossPay = hours * payRate;
taxAmount = grossPay * taxRate;
netPay = grossPay - taxAmount;
}
// increase index number
index++;
}
}
//*********************************************************************************************
// function to to print out timesheet
//*********************************************************************************************
void outTime(timeSheet *time, employeeRecord *employee, int &index){
index = 0; // set index back to zero to start at first array index
int empID = employee[index].IDnum; //employes id number
string name = employee[index].name; // employees name
float grossPay = time[index].grossPay; //employes's gross pay
float taxAmount = time[index].taxAmount; ////employes's tax deduction
float netPay = time[index].netPay; //employes's net pay
cout << "Pay Roll Report\n" << endl;
cout << setw(10) << left << "ID" << setw(30) << left << "Name";
cout << setw(10) << right << "Gross Pay" << setw(10) << right << "Tax";
cout << setw(10) << right << "Net Pay" << endl;
// for loop to display employee information.'
for (int i=0; i < 2; i++){
cout << setw(10) << left << empID << setw(30) << left << name;
cout << setw(10) << right << grossPay << setw(10) << right << taxAmount;
cout << setw(10) << right << netPay << endl;
index++;}
}