Metal Guitarist Forums banner
1 - 9 of 9 Posts

·
Mutes the Meat
Joined
·
10,236 Posts
Discussion Starter · #1 ·
I'm just leaving this here before I go to bed. The program is a payroll calculator that can handle overtime. It's for a school assignment. I'm getting some odd outputs punching in random values. Numbers are showing as negative, some are popping up all truncated-like-odd. I'm sure that there's some fault in logic on my part, but maybe you guys can give me a pointer or two (see what I did there?)

This Code:

Code:
//Main class for payroll calculator
//IT218 Final Project
//Programmer: Mike Tremblay

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class employeeObj{
      public:
             //Functions
             
             public:
             
             virtual void getHoursOver(){
                     hoursOver = hoursWorked-40;}
             virtual void getHoursUnder(){
                     if(hoursWorked>40)
                         hoursUnder=40;}
             virtual void getOverPay(){
                       overPay = hoursOver*payRate*1.5;
                       }
             virtual void getUnderPay(){
                     underPay = hoursUnder*payRate;
                     }
             virtual void getTotalPay(){
                     totalPay = overPay+underPay;
                     }           
             //Variables 
             
             public:
                    
             string empName; //Name of the employee
             int hoursWorked;//Total hours worked
             float payRate; // Employee's pay rate
             
             int hoursOver;// Hours worked over 40
             int hoursUnder;// Hours worked under or equal to 40
             
             double overPay; // Total overtime pay
             double underPay;//Total non-overtime pay
             double totalPay;// Total pay                 
             };

int main(){
    system("cls");
    
    cout<<"\nWelcome to the Employee Pay Center\n\n";
    cout<<"Datamax, Inc.\n\n";
    
    //Create employee objects
    employeeObj emp1;
    employeeObj emp2;
    employeeObj emp3;
    
    
    //Prompt for information
    cout<<"Please enter the name of the first employee: ";
    cin>> emp1.empName;
    cout<<"\nPlease enter the hours worked: ";
    cin>> emp1.hoursWorked;
    cout<<"\nPlease enter the employee's hourly wage: ";
    cin>>emp1.payRate;
    
    cout<<"\n\nPlease enter the name of the second employee: ";
    cin>> emp2.empName;
    cout<<"\nPlease enter the hours worked: ";
    cin>>emp2.hoursWorked;
    cout<<"\nPlease enter the employee's hourly wage: ";
    cin>>emp2.payRate;
    
    cout<<"\n\nPlease enter the name of the third employee: ";
    cin>> emp3.empName;
    cout<<"\nPlease enter the hours worked: ";
    cin>> emp3.hoursWorked;
    cout<<"\nPlease enter the employee's hourly wage: ";
    cin>>emp3.payRate;
    
    //Calculate some things
    
    emp1.getHoursOver();
    emp1.getHoursUnder();
    emp1.getOverPay();
    emp1.getUnderPay();
    emp1.getTotalPay();
    
    emp2.getHoursOver();
    emp2.getHoursUnder();
    emp2.getOverPay();
    emp2.getUnderPay();
    emp2.getTotalPay();
    
    emp3.getHoursOver();
    emp3.getHoursUnder();
    emp3.getOverPay();
    emp3.getUnderPay();
    emp3.getTotalPay();
    
    //Output information
    
    cout<<"\nThe Payroll Values are as Follows:\n\n";
    
    cout<<"Employee Name: "<<emp1.empName;
    cout<<"\nBase Pay: "<<emp1.payRate;
    cout<<"\nHours in Overtime: "<<emp1.hoursOver;
    cout<<"\nOvertime Pay Amount: "<<emp1.overPay;
    cout<<"\nPay Before Overtime: "<<emp1.underPay;
    cout<<"\nTotal Pay: "<<emp1.totalPay;
    
    cout<<"\n\nEmployee Name: "<<emp2.empName;
    cout<<"\nBase Pay: "<<emp2.payRate;
    cout<<"\nHours in Overtime: "<<emp2.hoursOver;
    cout<<"\nOvertime Pay Amount: "<<emp2.overPay;
    cout<<"\nPay Before Overtime: "<<emp2.underPay;
    cout<<"\nTotal Pay: "<<emp2.totalPay;
    
    cout<<"\n\nEmployee Name: "<<emp3.empName;
    cout<<"\nBase Pay: "<<emp3.payRate;
    cout<<"\nHours in Overtime: "<<emp3.hoursOver;
    cout<<"\nOvertime Pay Amount: "<<emp3.overPay;
    cout<<"\nPay Before Overtime: "<<emp3.underPay;
    cout<<"\nTotal Pay: "<<emp3.totalPay;
    
    //Some extra variables to hold the grand totals.
    
    double salGTotal; //Salary grand total
    int hoursGTotal; //Hours grand total
    int othoursGTotal; //OT Hours grand total
    
    //Addy Uppy
    
    salGTotal = emp1.totalPay+emp2.totalPay+emp3.totalPay;
    hoursGTotal = emp1.hoursWorked+emp2.hoursWorked+emp3.hoursWorked;
    othoursGTotal = emp1.hoursOver+emp2.hoursOver+emp3.hoursOver;
    
    cout<<"\n\n##########";
    cout<<"\nTotal of all employee salaries: "<<salGTotal;
    cout<<"\nTotal employee hours worked: "<<hoursGTotal;
    cout<<"\nTotal overtime hours worked: "<<othoursGTotal;
    cout<<"\n##########\n";    
    
};
Produces this output:

Code:
Welcome to the Employee Pay Center

Datamax, Inc.

Please enter the name of the first employee: Mike

Please enter the hours worked: 45

Please enter the employee's hourly wage: 15


Please enter the name of the second employee: Mary

Please enter the hours worked: 30

Please enter the employee's hourly wage: 29


Please enter the name of the third employee: John

Please enter the hours worked: 50

Please enter the employee's hourly wage: 56

The Payroll Values are as Follows:

Employee Name: Mike
Base Pay: 15
Hours in Overtime: 5
Overtime Pay Amount: 112.5
Pay Before Overtime: 600
Total Pay: 712.5

Employee Name: Mary
Base Pay: 29
Hours in Overtime: -10
Overtime Pay Amount: -435
Pay Before Overtime: 5.82683e+010
Total Pay: 5.82683e+010

Employee Name: John
Base Pay: 56
Hours in Overtime: 10
Overtime Pay Amount: 840
Pay Before Overtime: 2240
Total Pay: 3080

##########
Total of all employee salaries: 5.82683e+010
Total employee hours worked: 125
Total overtime hours worked: 5
##########
I have never seen this before :/
 

·
Squirrel...
Joined
·
3,157 Posts
Well, for the negative amount in overtime hours and pay, the problem seems to be the part of your code that grabs the amount of hours over 40 that are worked.

Code:
virtual void getHoursOver(){
                     hoursOver = hoursWorked-40;}
In the case of Mary, it's taking 30 hours, and subtracting 40 from it. It seems you'd have to specify to only deduct the 40 hours, if more than 40 hours are worked.

Code:
virtual void getHoursOver(){ 
                 if(hoursWorked>=40)
                     hoursOver = hoursWorked-40;}
Keep in mind that I'm also a programming student, still working in Java, and I've never touched C++. So if anyone determines that I'm wrong, go easy. :lol:
 

·
Squirrel...
Joined
·
3,157 Posts
Also, I don't see off hand where you're determining the amount of hours that are worked if it's less than 40 (like in the case of Mary) Which is likely why her pay before overtime is FUBAR'd.

You have

Code:
virtual void getHoursUnder(){
                     if(hoursWorked>40) //Should probably be >= by the way.
                         hoursUnder=40;}
Which gives you the hoursUnder if more than 40 hours are worked (meaning overtime is paid)

You should add an else statement in there that states that hoursUnder=hoursWorked for cases where the employee didn't work any overtime.
 

·
Premium Member
Joined
·
278 Posts
Also, I don't see off hand where you're determining the amount of hours that are worked if it's less than 40 (like in the case of Mary) Which is likely why her pay before overtime is FUBAR'd.

You have

Code:
virtual void getHoursUnder(){
                     if(hoursWorked>40) //Should probably be >= by the way.
                         hoursUnder=40;}
Which gives you the hoursUnder if more than 40 hours are worked (meaning overtime is payed)

You should add an else in there that states that hoursUnder=hoursWorked for cases where the employee didn't work any overtime.
This. In C/C++, if you don't initialize a variable, it gets filled what whatever crap happens to be in memory at the time, which is why you're getting nonsense answers for anyone who worked less than 40 hours,
 

·
Mutes the Meat
Joined
·
10,236 Posts
Discussion Starter · #7 ·
Wirelessly posted (iPhone : Mozilla/5.0 (Linux; U; Android 2.2.1; en-us; Droid Build/FRG83D) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1)

Ahh thanks. I knew it had to be something simple that I was overlooking. Stupid mistake really. Should be able to fix it all up tomorrow.

When I coded it, I wasn't taking into account that some employees would not work a whole 40 hours.
 
1 - 9 of 9 Posts
Top