| | |
Need help with payroll programm with structures C++
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2009
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
CPP Syntax (Toggle Plain Text)
#include <iostream> #include<sstream> #include <string> #include <iomanip> using namespace std; const float HOURLYPAYRATE=18.75; const float COMMISSIONRATE=1000.00; enum PayType {salary, hourly, commission}; union PayRate{ long salary; float hours; int commission; }; struct Employee { char id[4]; char name[80]; PayType type; PayRate rate; float grossPay; }; int getEmployeeData(Employee* e, int size); float calculateGrossPay(Employee [], int); void displayEmployeeInfo(Employee [], int size); int maxPayIndex(Employee[], int size); int main() { int MAX=0; cout<<"Enter maximum number of employees to enter?"<<endl; cin>>MAX; Employee* emp; emp=new Employee[MAX]; if(emp==NULL) { cerr<<"couldn't allocate memory"<<endl; return 1; } MAX=getEmployeeData(emp,MAX); emp->grossPay=calculateGrossPay(emp,MAX); displayEmployeeInfo(emp,MAX); delete [] emp; return 0; } int getEmployeeData(Employee* e, int size) { bool test=true; int count=0; for(int i=0; i<size && test ; i++) { count++; stringbuf buffer; cout << "Enter the employee's id (Enter to quit): "; cin.get(buffer); cout << buffer.str() << endl; if ( buffer.str().empty() ) { return count; } else { cout << "Enter name: "; cin.ignore(); cin.getline((e+i)->name, 80); cout << "Enter payroll status: "; char input; cin >> input; cin.ignore(); if (input=='h'|| input=='H') { (e+i)->type=hourly; cout<<"Enter number of hours worked this month: "; cin>>(e+i)->rate.hours; } else if ( input== 's' || input =='S') { (e+i)->type=salary; cout<<"Enter monthly salary: "; cin>>(e+i)->rate.salary; } else if( input=='c'|| input=='C') { (e+i)->type=commission; cout<<"Enter total sales this month: "; cin>>(e+i)->rate.commission; } } } return count; } float calculateGrossPay(Employee e[], int size) { for(int i=0; i<size; i++) { if(e[i].type==salary) { e[i].grossPay= (float(e[i].rate.salary)); } else if(e[i].type==hourly) { e[i].grossPay=(HOURLYPAYRATE * e[i].rate.hours); } else if(e[i].type==commission) { e[i].grossPay=float(COMMISSIONRATE + (0.06 * e[i].rate.commission)); } } return e->grossPay; } void displayEmployeeInfo(Employee e[], int size) { cout<<" ID Name PayrollStatus GrossPay\n"; cout<<endl; for(int i=0; i<size; i++) { cout <<setw(4)<<e[i].id<<setw(10)<<" "<<e[i].name; if(e[i].type==hourly) { cout<<setw(10)<<"Hourly"; cout << fixed << showpoint << setprecision(2); cout<<setw(10)<< e[i].grossPay<< endl; } else if(e[i].type==salary) { cout<<setw(10)<<"Salaried"; cout<<fixed<<showpoint<<setprecision(2); cout<<setw(10)<<e[i].grossPay<<endl; } else if(e[i].type==commission) { cout<<setw(10)<<"Commisioned"; cout<<fixed<<showpoint<<setprecision(2); cout<<setw(10)<<e[i].grossPay<<endl; } } int max =maxPayIndex(e,size); cout<<"the employee with maxium gross pay is"<<e[max].name<<endl; } int maxPayIndex(Employee e[], int size) { float largest=e[0].grossPay; int count=0; for(int i=1; i<size; i++) { count++; if(e[i].grossPay>largest) largest=e[i].grossPay; } return count+1; }
Last edited by GSPprog; May 31st, 2009 at 8:52 am.
•
•
Join Date: Feb 2008
Posts: 638
Reputation:
Solved Threads: 46
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
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
A common, simple model for entering data till there is no more is something like:
C++ Syntax (Toggle Plain Text)
struct empl {string name; int ID, department; }; empl workers[10] int limit = 10; int count = 0; while( count < limit && getline( cin, workers[count].name ) { cin >> workers[count].ID; cin >> workers[count].dept; count++; }
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Mar 2009
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- Need help with Java Payroll Program Part 2 (Java)
- how to get the last key pressed without stopping the programm ? (C)
- Creating dynamic array structures (C++)
- C++ structures (C++)
- Read and writing strings from structures (C++)
- hi friends pls help me out (C++)
- Problems with displaying total payroll and allowing user to quit by entering a neg. (C++)
- Java Collections: ADTs, Data Structures & Algorithms (Java)
Other Threads in the C++ Forum
- Previous Thread: WinAPI Dialogs class without MFC
- Next Thread: nested while loops not exiting
Views: 337 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






