Need help displaying when user enters negative number?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 18
Reputation: hill0ster is an unknown quantity at this point 
Solved Threads: 0
hill0ster hill0ster is offline Offline
Newbie Poster

Need help displaying when user enters negative number?

 
0
  #1
Nov 7th, 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. This function also has to do error checking so the user cannot enter '1xv'. I got that part but need help with displaying when user enters a negative number.
#2) I can't seem to figure out Total Payroll to display properly, I get junk values.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Need help displaying when user enters negative number?

 
0
  #2
Nov 7th, 2004
Stop making new threads for the same stupid question! I have a sound I'd like you to hear:

*plonk*
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 44
Reputation: jigvesh is an unknown quantity at this point 
Solved Threads: 0
jigvesh jigvesh is offline Offline
Light Poster

Re: Need help displaying when user enters negative number?

 
0
  #3
Nov 7th, 2004
Hey dont get depressed by that comment. The solution is you can use exceptions. For example, you can throw an exception when the id<0 and on catching the exception display the details you need. Hope i am clear. If not tell me i will try to send you the code.
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: Need help displaying when user enters negative number?

 
0
  #4
Nov 7th, 2004
Seriously, it's called double posting, and it's not encouraged in this forum at all.

Closing this thread; please continue conversation in the thread already created.
Alex Cavnar, aka alc6379
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC