need help with C++ project for college

This is a discussion on need help with C++ project for college within the C++ Programming forums, part of the General Programming Boards category; i have to make a program to calculate certain wages and salaries for different employees.. my question is how do ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    3

    Post need help with C++ project for college

    i have to make a program to calculate certain wages and salaries for different employees.. my question is how do i let the user input as many different employees as they want and for me to output them? because now when i do it, it just keeps replacing it with the new data and only outputs the last one i entered.. here is the code i have so far...its kind of confusing to read this way..if u IM me on aol ill email it to ..thanks for all help

    #include <iostream.h>
    #include <iomanip.h>
    #include <conio.h>
    const int SIZE=20, ALLWD_HRS=40;
    char last_name[SIZE], first_name[SIZE];
    char exit;
    int emp_type, pieces;
    double st_tax, fed_tax;
    const double STATE_TAX=.06, FEDERAL_TAX=.12, HRLY_WAGE=12.50, TIME_HALF=18.75;
    const double MANGR_ST_TAX=52.50, MANGR_FED_TAX=157.50, MANGR_GROSS=875.00;
    const double MANGR_SAL=717.50, PIECES_COST=1.125;
    double total_gross_salary=0, hrs_worked=0, over_time=0,wage=0;
    double gross=0, salary=0;

    int main()
    {
    cout.precision(2);

    cout<<"This program will calculate a person's weekly gross salary based\n"
    "on his job type. It will then calculate the state and federal taxes\n"
    "and then output the person's final salary. Also it will output the \n"
    "total gross salary of all the salaries entered.\n\n\n\n";


    do
    {
    cout<<"Please enter the first name of the employee: ";
    cin.getline(first_name, SIZE);
    cin.ignore(80, '\n');

    cout<<"\n\nPlease enter the last name of the employee: ";
    cin.getline(last_name, SIZE);
    cin.ignore(80, '\n');

    clrscr();
    cout<<"\n\n\n\n";
    cout<<" ***********************\n";
    cout<<" **EMPLOYEE CODE CHART**\n";
    cout<<" ***********************\n";
    cout<<" ** 1= Manager **\n";
    cout<<" ** 2= Hourly Worker**\n";
    cout<<" ** 3= Piece Worker **\n";
    cout<<" ***********************\n";
    cout<<"\n\nPlease enter employee type by using the code for each type: ";

    cin>>emp_type;

    switch(emp_type)
    {
    case 1:
    {
    gross=MANGR_GROSS;
    salary=MANGR_SAL;
    st_tax=MANGR_ST_TAX;
    fed_tax=MANGR_FED_TAX;
    total_gross_salary=MANGR_GROSS+total_gross_salary;

    break;
    }
    case 2:
    {
    cout<<"\n\nEnter number of hours worked this week: ";
    cin>>hrs_worked;

    if (hrs_worked > ALLWD_HRS)
    {
    over_time=(hrs_worked-ALLWD_HRS)*(TIME_HALF);
    gross=wage+over_time;
    }
    else
    {
    gross=(hrs_worked*HRLY_WAGE);

    }
    total_gross_salary=gross+total_gross_salary;

    st_tax=gross*STATE_TAX;

    fed_tax=gross*FEDERAL_TAX;

    salary=gross-(st_tax+fed_tax);

    break;
    }
    case 3:
    {
    cout<<"\n\nEnter the number of pieces produced this week ";
    cin>>pieces;
    gross=pieces*PIECES_COST;
    total_gross_salary=gross+total_gross_salary;
    st_tax=gross*STATE_TAX;
    fed_tax=gross*FEDERAL_TAX;
    salary=gross-(st_tax+fed_tax);

    break;
    }
    default:
    {
    cout<<"\n\nNot a correct employee code ";

    break;
    }


    }
    cout<<"\n\nDo you want to continue?: ";
    cin>>exit;

    }while (exit != 'N'||exit != 'n ');

    cout<<"\n\n\n\n";
    cout<<"Name Emp. Pieces Hrs. State Fed. Gross Salary\n";
    cout<<" Type Wkd. Tax Tax \n";
    cout<<"==============|===|=======|=====|=======|== ===|==========|========|\n";
    cout<<setw(6)<<last_name<<setw(6)<<first_name<<set w(4)<<emp_type<<setw(4)<<pieces;
    cout<<setw(4)<<hrs_worked<<setw(4)<<st_tax<<setw(4 )<<fed_tax<<setw(4)<<'$'<<gross;
    cout<<setw(4)<<'$'<<salary'\n';

    return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    use a container to hold the input and then manipulate the container later. If you know about struct/class this would be a good place to use that technique as well. an array or list of your struct/class type would work well. You could use a group of parallel arrays, but it will be a hassle. Maybe that's the point your instructor will try to make...force you to do it the hard way, and then show you the easier solution.

    Also, please read the sticky posts to learn how to use the code tags to make your code more readable on this site.

  3. #3
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    seames like u want to have an ability to add unlimited amount of "users" (salaries).....

    well "arrays" wouldn't work for that b/c they are statically allocated and have a limit to its size......

    however "linked lists" for example, will give u a capability of unlimited amount users, b/c the whole thing happens dynamicaly......

    if you know linked lists, which i have a feeling you don't, then u'll be o.k.

    good luck...

    Regards,
    matheo917

  4. #4
    Registered User Liam Battle's Avatar
    Join Date
    Jan 2002
    Posts
    114
    well you dont have to use linked lists, you can use arrays, but make you you dynamically allocate them...

    ie: if Employee was a class then:

    Employee *myapp;

    myapp = new Employee [my_size];

    that would dynamicall allocate whatever size you want, if you want to add another one to that, just create a temp employee array with my_size +1, then save the old one into it, delete the old array and allocate it to my_size +1, copy the temp array into it and then delete the temp array... it may sound confusing but its small, and it works... if you dont need linked lists, you dont have to use em.

    LB0: * Life once school is done
    LB1: N <- WakeUp;
    LB2: N <- C++_Code;
    LB3: N >= Tired : N <- Sleep;
    LB4: JMP*-3;

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    23
    Originally posted by Liam Battle
    well you dont have to use linked lists, you can use arrays, but make you you dynamically allocate them...

    ie: if Employee was a class then:

    Employee *myapp;

    myapp = new Employee [my_size];

    that would dynamicall allocate whatever size you want, if you want to add another one to that, just create a temp employee array with my_size +1, then save the old one into it, delete the old array and allocate it to my_size +1, copy the temp array into it and then delete the temp array... it may sound confusing but its small, and it works... if you dont need linked lists, you dont have to use em.

    Your describing a vector...in that case there is a standard template library for it. It is also more efficent to DOUBLE the size of the new array instead of increasing it by one. Just think of how many times you gotta copy if say you have to increase it 100 times? If you double it you cut the number of times you gotta copy in almost half.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    223
    a mid level approach is also an option...

    create and employee class

    const MAX_EMPLOYEES = 1000;
    int iEmployeeCount = 0;

    class CEmployee{
    public:
    CEmployee(){

    //initialize your variables strFname, strLname, etc
    };

    ConsoleIn()
    {
    //code for input of fname, lname, wages... etc
    cout << "\nLast Name? " << flush;
    cin >> strLName;
    cout << "\nFirst Name?" << flush;
    cin >> strFName;

    };


    string strFname;
    string strLname;
    float flWages;
    };

    //array of CEmployee pointers
    CEmployee* cEmployees[MAX_EMPLOYEES];


    void main(void)
    {
    bool bQuit = false;

    while(10
    {


    cEmployees[ iEmployeeCount ] = new CEmployee;

    if( cEmployees[ iEmployeeCount ] )
    cEmployees[ iEmployeeCount ]->ConsoleIn();

    }
    }
    zMan

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    223
    a mid level approach is also an option... This approach is not quite as sophisticated as the other ones mentioned but for a class room assignment it should do the trick.

    You can hardwire the highest number of Employees that you think you will need...
    Then create them as you go.... this way you can loop through them.... the only draw back is that the max number is still fixed but you are only occupying enough memory of the pointers max + the memory for each object. You will also have to delete them for clean up....
    THIS CODE IS ONLY A SAMPLE AND WITH SOME DEBUGGING IT WILL WORK.

    Code:
    const MAX_EMPLOYEES = 1000;
    int     iEmployeeCount = 0;
    
    class CEmployee{
    public:
           CEmployee(){
    
              //initialize your variables strFname, strLname, etc
           };
    
           ConsoleIn()
           {
               //code for input of fname, lname, wages... etc
               cout << "\nLast Name? " << flush;
               cin >> strLName;
                cout << "\nFirst Name?" << flush;
                cin >> strFName;
                ......
    
           };
    
    
           string strFname;
           string strLname;
           float   flWages;
           int iEmployeeType;
    };
    
    //array of CEmployee pointers
    CEmployee* cEmployees[MAX_EMPLOYEES];
    
    
    void main(void)
    {
           bool bQuit = false;
           char chYesNo;
    
           while(1)
           {
                  cout << "\nAnother Employee?(Y/N) " << flush;
                 cin >> chYesNo;
                   
                  if( chYesNo != 'Y')
                         break;
                  
                  
                   cEmployees[ iEmployeeCount ] = new CEmployee;
                  
                   if( cEmployees[ iEmployeeCount ] )
                            cEmployees[ iEmployeeCount  ]->ConsoleIn();
                     iEmployeeCount++;
    
            }
    }
    zMan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 10:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 02:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 08:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21