943,940 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2065
  • C++ RSS
Nov 6th, 2004
0

Problems with displaying total payroll and allowing user to quit by entering a neg.

Expand Post »
Quote ...
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)
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. #include <string>
  5. int getEmployeeData (string[], char[], const int, double[]);
  6. float calculateGrossPay(string[], char[], double[], int, double[]);
  7. void displayEmployeeInfo (string[], char[], int, double[], double[]);
  8. bool tryAgain();
  9. bool isValidStatus(char);
  10. bool isValidID (string);
  11.  
  12.  
  13. int main()
  14. {
  15. const int numEmp = 4;
  16. string ID[numEmp];
  17. char pyrllSt[numEmp];
  18. double grossPay[numEmp];
  19. double netPay[numEmp];
  20. double totalPayroll = 0;
  21. getEmployeeData(ID, pyrllSt, numEmp, grossPay);
  22. calculateGrossPay(ID, pyrllSt, grossPay, numEmp, netPay);
  23.  
  24. return 0;
  25. }
  26.  
  27.  
  28. int getEmployeeData (string ID[], char pyrllSt[], const int numEmp, double grossPay[])
  29. {
  30. for (int index = 0; index < numEmp; index++)
  31. {
  32. do
  33. {
  34. cout << "Enter employee ID:" << endl;
  35. cin >> ID[index];
  36. } while(!isValidID(ID[index]));
  37.  
  38. do
  39. {
  40. cout << "Enter payroll status:" << endl;
  41. cin >> pyrllSt[index];
  42. }while (!isValidStatus(pyrllSt[index]));
  43.  
  44. if (pyrllSt[index] == 's' || pyrllSt[index] == 'S')
  45. {
  46. cout << "Enter the monthly salary:" << endl;
  47. }
  48.  
  49. else if (pyrllSt[index] == 'h' || pyrllSt[index] == 'H')
  50. {
  51. cout << "Enter the number of hours worked this month:" << endl;
  52. }
  53.  
  54. else if (pyrllSt[index] == 'c' || pyrllSt[index] == 'C')
  55. {
  56. cout << "Enter total sales for this month: " << endl;
  57. }
  58. cin >> grossPay[index];
  59. }
  60. return grossPay[index];
  61. }
  62.  
  63. float calculateGrossPay (string ID[], char pyrllSt[], double grossPay[] , const int numEmp, double netPay[])
  64. {
  65. for (int index = 0; index < numEmp; index++)
  66. {
  67. if (pyrllSt[index] == 's' || pyrllSt[index] == 'S')
  68. {
  69. netPay[index] = grossPay[index];
  70. }
  71. if (pyrllSt[index] == 'h' || pyrllSt[index] == 'H')
  72. {
  73. netPay[index] = grossPay[index] * 18.75;
  74. }
  75. if (pyrllSt[index] == 'c' || pyrllSt[index] == 'C')
  76. {
  77. netPay[index] = (grossPay[index] * 0.06) + 1000.00;
  78. }
  79. }
  80. displayEmployeeInfo (ID, pyrllSt, numEmp, netPay, grossPay);
  81. return netPay[index];
  82. }
  83.  
  84. void displayEmployeeInfo (string ID[], char pyrllSt[], const int numEmp, double netPay[],double grossPay[])
  85. {
  86. cout << "Employee ID " << "Payroll Status" << " GrossPay" << endl;
  87. cout << fixed << showpoint << setprecision(2);
  88. for (int index = 0; index < numEmp; index++)
  89. {
  90. if (pyrllSt[index] == 's' ||pyrllSt[index] == 'S')
  91. {
  92. cout << ID[index] << setw(20) << "Salaried" << setw(11) << "$" << netPay[index] << endl;
  93. }
  94. if (pyrllSt[index] == 'c' || pyrllSt[index] == 'C')
  95. {
  96. cout << ID[index] << setw(24) << "Commissioned" << setw(7) << "$" << netPay[index] << endl;
  97. }
  98. if (pyrllSt[index] == 'h' || pyrllSt[index] == 'H')
  99. {
  100. cout << ID[index] << setw(18) << "Hourly" << setw(13) << "$" << netPay[index] << endl;
  101. }
  102.  
  103. }
  104.  
  105. //float totalPayroll += grossPay[index];
  106. //cout << "Total Payroll for this month: " << "$" << totalPayroll << endl;
  107.  
  108. }
  109.  
  110. bool tryAgain()
  111. {
  112. char quit;
  113.  
  114. cout << "Do you want to try again or quit?" << endl;
  115. cout << "Type Q or q to quit, any other key to continue: " << endl;
  116. cin.get(quit);
  117.  
  118. if (quit != '\n')
  119. {
  120. cin.ignore();
  121. }
  122.  
  123. cin.get(quit);
  124.  
  125. if (quit == 'q' || quit == 'Q')
  126. {
  127. cout << "*** Program Terminated ***" << endl;
  128. exit (0);
  129. return false;
  130.  
  131. }
  132.  
  133. else
  134. {
  135. return true;
  136. }
  137. }
  138.  
  139. bool isValidStatus(char pyrllSt)
  140. {
  141. if (pyrllSt == 's' || pyrllSt == 'S' || pyrllSt == 'h' || pyrllSt == 'H' || pyrllSt == 'c' || pyrllSt == 'C')
  142. {
  143. return true;
  144. }
  145. else
  146. {
  147. cout << "*** Invalid payroll status ***" << endl;
  148. tryAgain();
  149. cout << "Re-enter 's' or 'S' for salaried, 'h' or 'H' for hourly," << endl;
  150. cout << "'c' or 'C' for commissioned." << endl;
  151. return false;
  152. }
  153. }
  154.  
  155. bool isValidID(string ID)
  156. {
  157. for (int index = 0; index < ID.length(); index++)
  158. {
  159. if(ID[0] < 48 || ID[0] > 57)
  160. {
  161. cout << "*** Invalid employee ID ***" << endl;
  162. return tryAgain();
  163. }
  164. }
  165.  
  166. if(ID.length() != 3)
  167. {
  168. cout << "*** Invalid employee ID ***" << endl;
  169. return false;
  170. }
  171. else
  172. {
  173. return true;
  174. }
  175. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hill0ster is offline Offline
18 posts
since Oct 2004
Nov 6th, 2004
0

Re: Problems with displaying total payroll and allowing user to quit by entering a neg.

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).
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Mathematical formulas used in C++ ?
Next Thread in C++ Forum Timeline: To learn faster for me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC