Need help with payroll programm with structures C++

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2009
Posts: 2
Reputation: GSPprog is an unknown quantity at this point 
Solved Threads: 0
GSPprog GSPprog is offline Offline
Newbie Poster

Need help with payroll programm with structures C++

 
0
  #1
May 31st, 2009
user puts in the number of employees but inputs less employees. He terminates the loop
by pressing the ENTER key. How do i do that. I tried strlen,, and even trid atoi but i dont know how to terminated
it. Also when it terminated its just uses that many employees that were entered and outputs the result.

The below method give me garbage. I need the return statment
to give me the count so i can display the results for the employees that were entered.

  1.  
  2. #include <iostream>
  3. #include<sstream>
  4. #include <string>
  5.  
  6. #include <iomanip>
  7.  
  8. using namespace std;
  9.  
  10. const float HOURLYPAYRATE=18.75;
  11. const float COMMISSIONRATE=1000.00;
  12.  
  13. enum PayType {salary, hourly, commission};
  14.  
  15. union PayRate{
  16. long salary;
  17. float hours;
  18. int commission;
  19. };
  20.  
  21. struct Employee {
  22.  
  23. char id[4];
  24. char name[80];
  25.  
  26. PayType type;
  27. PayRate rate;
  28.  
  29. float grossPay;
  30.  
  31. };
  32.  
  33.  
  34. int getEmployeeData(Employee* e, int size);
  35. float calculateGrossPay(Employee [], int);
  36. void displayEmployeeInfo(Employee [], int size);
  37. int maxPayIndex(Employee[], int size);
  38.  
  39.  
  40.  
  41. int main()
  42. {
  43.  
  44.  
  45.  
  46. int MAX=0;
  47.  
  48.  
  49.  
  50. cout<<"Enter maximum number of employees to enter?"<<endl;
  51. cin>>MAX;
  52.  
  53. Employee* emp;
  54.  
  55. emp=new Employee[MAX];
  56. if(emp==NULL)
  57. {
  58. cerr<<"couldn't allocate memory"<<endl;
  59. return 1;
  60. }
  61.  
  62. MAX=getEmployeeData(emp,MAX);
  63. emp->grossPay=calculateGrossPay(emp,MAX);
  64. displayEmployeeInfo(emp,MAX);
  65. delete [] emp;
  66. return 0;
  67. }
  68.  
  69.  
  70. int getEmployeeData(Employee* e, int size)
  71. {
  72.  
  73. bool test=true;
  74. int count=0;
  75.  
  76.  
  77.  
  78. for(int i=0; i<size && test ; i++)
  79. {
  80. count++;
  81.  
  82. stringbuf buffer;
  83. cout << "Enter the employee's id (Enter to quit): ";
  84. cin.get(buffer);
  85. cout << buffer.str() << endl;
  86. if ( buffer.str().empty() )
  87. {
  88. return count;
  89. }
  90. else
  91. {
  92. cout << "Enter name: ";
  93. cin.ignore();
  94. cin.getline((e+i)->name, 80);
  95. cout << "Enter payroll status: ";
  96. char input;
  97. cin >> input;
  98. cin.ignore();
  99. if (input=='h'|| input=='H')
  100. {
  101. (e+i)->type=hourly;
  102. cout<<"Enter number of hours worked this month: ";
  103. cin>>(e+i)->rate.hours;
  104. }
  105. else if ( input== 's' || input =='S')
  106. {
  107. (e+i)->type=salary;
  108. cout<<"Enter monthly salary: ";
  109. cin>>(e+i)->rate.salary;
  110. }
  111.  
  112. else if( input=='c'|| input=='C')
  113. {
  114. (e+i)->type=commission;
  115. cout<<"Enter total sales this month: ";
  116. cin>>(e+i)->rate.commission;
  117. }
  118. }
  119. }
  120.  
  121. return count;
  122. }
  123.  
  124.  
  125. float calculateGrossPay(Employee e[], int size)
  126. {
  127. for(int i=0; i<size; i++)
  128. {
  129.  
  130. if(e[i].type==salary)
  131. {
  132. e[i].grossPay= (float(e[i].rate.salary));
  133.  
  134. }
  135. else if(e[i].type==hourly)
  136. {
  137. e[i].grossPay=(HOURLYPAYRATE * e[i].rate.hours);
  138. }
  139. else if(e[i].type==commission)
  140. {
  141. e[i].grossPay=float(COMMISSIONRATE + (0.06 * e[i].rate.commission));
  142. }
  143. }
  144.  
  145. return e->grossPay;
  146.  
  147. }
  148.  
  149.  
  150. void displayEmployeeInfo(Employee e[], int size)
  151. {
  152. cout<<" ID Name PayrollStatus GrossPay\n";
  153. cout<<endl;
  154. for(int i=0; i<size; i++)
  155. {
  156. cout <<setw(4)<<e[i].id<<setw(10)<<" "<<e[i].name;
  157. if(e[i].type==hourly)
  158. {
  159. cout<<setw(10)<<"Hourly";
  160. cout << fixed << showpoint << setprecision(2);
  161. cout<<setw(10)<< e[i].grossPay<< endl;
  162. }
  163. else if(e[i].type==salary)
  164. {
  165. cout<<setw(10)<<"Salaried";
  166. cout<<fixed<<showpoint<<setprecision(2);
  167. cout<<setw(10)<<e[i].grossPay<<endl;
  168.  
  169. }
  170. else if(e[i].type==commission)
  171. {
  172. cout<<setw(10)<<"Commisioned";
  173. cout<<fixed<<showpoint<<setprecision(2);
  174. cout<<setw(10)<<e[i].grossPay<<endl;
  175. }
  176.  
  177.  
  178.  
  179. }
  180. int max =maxPayIndex(e,size);
  181. cout<<"the employee with maxium gross pay is"<<e[max].name<<endl;
  182. }
  183.  
  184. int maxPayIndex(Employee e[], int size)
  185. {
  186. float largest=e[0].grossPay;
  187. int count=0;
  188. for(int i=1; i<size; i++)
  189. {
  190. count++;
  191. if(e[i].grossPay>largest)
  192. largest=e[i].grossPay;
  193. }
  194.  
  195. return count+1;
  196. }
Last edited by GSPprog; May 31st, 2009 at 8:52 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 638
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Need help with payroll programm with structures C++

 
1
  #2
May 31st, 2009
Can you abstract the question to something like "how do you input everything the user types until the enter key is pressed?" or something like that? I'd say 3/4 of those lines are not related to your question. Please post
1) the shortest example possible that demonstrates the problem
2) a sample input
3) the current output
4) the expected output

and then we can probably help

Dave
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Need help with payroll programm with structures C++

 
0
  #3
Jun 1st, 2009
A common, simple model for entering data till there is no more is something like:
  1. struct empl
  2. {string name;
  3. int ID, department;
  4. };
  5. empl workers[10]
  6. int limit = 10;
  7. int count = 0;
  8. while( count < limit && getline( cin, workers[count].name )
  9. {
  10. cin >> workers[count].ID;
  11. cin >> workers[count].dept;
  12. count++;
  13. }
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 2
Reputation: GSPprog is an unknown quantity at this point 
Solved Threads: 0
GSPprog GSPprog is offline Offline
Newbie Poster

Re: Need help with payroll programm with structures C++

 
0
  #4
Jun 1st, 2009
Thanks for all the help .
What i am trying to do is basically detect when an Enter key was pressed, and return the number of employees that were entered. This happens in getEmployeeData right when the id is entered. The program dynamically gets the number of employees but if the user decides that they just want to input less number of employees then they end their input with pressing Enter. How do I accomplish this? What the program does is return the count of employees entered and calls the function to calculate and display the data for gross pay . For example if the user is asked how many employees, they enter 5, but then after inputting the id, name, payrollstatus, and either their number of hours worked, monthly salary, or sales that month ... Then they input one more data for another employee. They decided they don't want to input any more info . So they press Enter when they are asked for the id. The program then outputs their info along with their gross Pay. this is accomplished by calling the displayEmployeeInfo. Also the maxPayINdex returns the index number of the array that corresponds to the employee with the highest grosspay.

Let me know if you need anymore explanation. So basically i am trying to terminate the input with an Enter key. I have tried getch
and also tried the buffer method but i get garbage after the Enter
key is pressed and the program terminates.
Reply With Quote Quick reply to this message  
Reply

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




Views: 337 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC