| | |
Problems with displaying total payroll and allowing user to quit by entering a neg.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 18
Reputation:
Solved Threads: 0
Problems with displaying total payroll and allowing user to quit by entering a neg.
0
#1 Nov 6th, 2004
•
•
•
•
I am having two problems with this code. #1) The user is to enter a three digit number for their employee ID. No problem there. But if the user enters a negative number, such as -1 the program is to end and display the total payroll for the month, total payroll is all the employees pay added together.
#2) I can't seem to figure out Total Payroll to display properly, I get junk values.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; #include <string> int getEmployeeData (string[], char[], const int, double[]); float calculateGrossPay(string[], char[], double[], int, double[]); void displayEmployeeInfo (string[], char[], int, double[], double[]); bool tryAgain(); bool isValidStatus(char); bool isValidID (string); int main() { const int numEmp = 4; string ID[numEmp]; char pyrllSt[numEmp]; double grossPay[numEmp]; double netPay[numEmp]; double totalPayroll = 0; getEmployeeData(ID, pyrllSt, numEmp, grossPay); calculateGrossPay(ID, pyrllSt, grossPay, numEmp, netPay); return 0; } int getEmployeeData (string ID[], char pyrllSt[], const int numEmp, double grossPay[]) { for (int index = 0; index < numEmp; index++) { do { cout << "Enter employee ID:" << endl; cin >> ID[index]; } while(!isValidID(ID[index])); do { cout << "Enter payroll status:" << endl; cin >> pyrllSt[index]; }while (!isValidStatus(pyrllSt[index])); if (pyrllSt[index] == 's' || pyrllSt[index] == 'S') { cout << "Enter the monthly salary:" << endl; } else if (pyrllSt[index] == 'h' || pyrllSt[index] == 'H') { cout << "Enter the number of hours worked this month:" << endl; } else if (pyrllSt[index] == 'c' || pyrllSt[index] == 'C') { cout << "Enter total sales for this month: " << endl; } cin >> grossPay[index]; } return grossPay[index]; } float calculateGrossPay (string ID[], char pyrllSt[], double grossPay[] , const int numEmp, double netPay[]) { for (int index = 0; index < numEmp; index++) { if (pyrllSt[index] == 's' || pyrllSt[index] == 'S') { netPay[index] = grossPay[index]; } if (pyrllSt[index] == 'h' || pyrllSt[index] == 'H') { netPay[index] = grossPay[index] * 18.75; } if (pyrllSt[index] == 'c' || pyrllSt[index] == 'C') { netPay[index] = (grossPay[index] * 0.06) + 1000.00; } } displayEmployeeInfo (ID, pyrllSt, numEmp, netPay, grossPay); return netPay[index]; } void displayEmployeeInfo (string ID[], char pyrllSt[], const int numEmp, double netPay[],double grossPay[]) { cout << "Employee ID " << "Payroll Status" << " GrossPay" << endl; cout << fixed << showpoint << setprecision(2); for (int index = 0; index < numEmp; index++) { if (pyrllSt[index] == 's' ||pyrllSt[index] == 'S') { cout << ID[index] << setw(20) << "Salaried" << setw(11) << "$" << netPay[index] << endl; } if (pyrllSt[index] == 'c' || pyrllSt[index] == 'C') { cout << ID[index] << setw(24) << "Commissioned" << setw(7) << "$" << netPay[index] << endl; } if (pyrllSt[index] == 'h' || pyrllSt[index] == 'H') { cout << ID[index] << setw(18) << "Hourly" << setw(13) << "$" << netPay[index] << endl; } } //float totalPayroll += grossPay[index]; //cout << "Total Payroll for this month: " << "$" << totalPayroll << endl; } bool tryAgain() { char quit; cout << "Do you want to try again or quit?" << endl; cout << "Type Q or q to quit, any other key to continue: " << endl; cin.get(quit); if (quit != '\n') { cin.ignore(); } cin.get(quit); if (quit == 'q' || quit == 'Q') { cout << "*** Program Terminated ***" << endl; exit (0); return false; } else { return true; } } bool isValidStatus(char pyrllSt) { if (pyrllSt == 's' || pyrllSt == 'S' || pyrllSt == 'h' || pyrllSt == 'H' || pyrllSt == 'c' || pyrllSt == 'C') { return true; } else { cout << "*** Invalid payroll status ***" << endl; tryAgain(); cout << "Re-enter 's' or 'S' for salaried, 'h' or 'H' for hourly," << endl; cout << "'c' or 'C' for commissioned." << endl; return false; } } bool isValidID(string ID) { for (int index = 0; index < ID.length(); index++) { if(ID[0] < 48 || ID[0] > 57) { cout << "*** Invalid employee ID ***" << endl; return tryAgain(); } } if(ID.length() != 3) { cout << "*** Invalid employee ID ***" << endl; return false; } else { return true; } }
Re: Problems with displaying total payroll and allowing user to quit by entering a neg.
0
#2 Nov 6th, 2004
totalPayroll += grossPay[index];
needs to go INSIDE the loop, so you add on to it on each iteration. Declare the float BEFORE the loop, of course (float totalPayroll = 0).
Why do you input the payroll ID as a string rather than a number? As a number, you could easily check for negative and being three digits (>=100, <=999).
needs to go INSIDE the loop, so you add on to it on each iteration. Declare the float BEFORE the loop, of course (float totalPayroll = 0).
Why do you input the payroll ID as a string rather than a number? As a number, you could easily check for negative and being three digits (>=100, <=999).
![]() |
Similar Threads
- Possibly using correct grammar?? (Java)
- can anyone please help me? i'm trying to fix a code that is doing an invoice in loops (C++)
- Displaying Average (C)
- Creating a Basic String Database (C++)
- Need help displaying when user enters negative number? (C++)
- hvving problems displaying after user inters negative number? (C++)
- Narue ROCKS!!! but need one last thing??? (C++)
- SLOT MACHINE PROGRAM... Help plsss! (C)
Other Threads in the C++ Forum
- Previous Thread: Mathematical formulas used in C++ ?
- Next Thread: To learn faster for me
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





