HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

Payroll program

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
payrollprogramstackoverflow

Problem

I wrote a program that calculates gross wages for employees. The program asks the user enter an employee's ID, hours worked and hourly pay rate. It then calculate the gross wage for that employee and store this employee’s information into a file, chosen by the user. It then asks the user if they would like to show the information that has been entered.

I would really appreciate any help from fellow peers on any pointers on how I could improve my program.

```
#include
#include
#include
#include
#include

using namespace std;

// Prototype functions
double calcWage ();
void getEmployeeInfor();
void printWages();
void printTable();
void repeat();
void read();

// Defining terms for the program.
int id,result;
double payRate, hours, wage;
ofstream outputFile;
ifstream inputFile;
string file,strings;

int main()
{

cout > file;
outputFile.open(file.c_str());

// Get employee information
getEmployeeInfor();
// Calculate the total wage
wage = calcWage();
// Prints the Table to the outputFile
printTable();
// Prints the wages to the output File
printWages();
// Ask if more informations wants to be added/ calculated
repeat();
// Call read function to read information to console
read ();

return 0;

}

// Function for asking user for Employee Id, pay rate, and the hours worked
void getEmployeeInfor()

{
cout > id >> payRate >> hours;

//Loop to check for valid numbers.
while ((id > id >> payRate >> hours;
}
}

// Function for calculating the Wage
double calcWage()
{
double wages;
return wages = payRate * hours;
}

// Function for printing information to the outputFile
void printWages()
{
outputFile > answer;
if (answer == 'y'|| answer == 'Y')
{
getEmployeeInfor(); // gets employee information
wage = calcWage(); // calculates wage
printWages(); // prints wage to the outputfile
// calls repeat function again;
repeat();
}
else
{
// Close the outputfile
output

Solution

First, you are using namespace std;. This is not usually a good idea because if you use more than one namespace, you may have functions that have the same name, and the compiler will not know which to use. More information can be found here.

Another potential problem is that you have a fair amount of global variables. You should keep your variables to as tight a scope as possible, so I would recommend creating at least some of these in main() and passing them to other functions as needed.

getEmployeeInfor(); should be getEmployeeInfo(); as info is the accepted abbreviation for information (it even passes my spell check).

You are using redundant comments:

// Get employee information
getEmployeeInfor();


Your naming is very good, and you don't need to say in a comment what you are clearly saying in your code.

Your indentation is a bit off:

void getEmployeeInfor()

{
 cout > id >> payRate >> hours;

 //Loop to check for valid numbers.
   while ((id > id >> payRate >> hours;
  }
}


The standard indentation (usually) is 4 spaces. I would write this like this:

void getEmployeeInfo()
{
    std::cout > id >> payRate >> hours;

    //Loop to check for valid numbers.
    while (id > id >> payRate >> hours;
    }
}


I cleaned the indentation and spacing up, I removed unnecessary braces from the while conditions, I specified that I am using the std namespace, and I changed the << endl section in the output to the newline character \n because you are using a string anyway.

Most of your other methods have indentation problems, but you should be able to clean them up using these tips.

Code Snippets

// Get employee information
getEmployeeInfor();
void getEmployeeInfor()

{
 cout << "Please enter your employee id, payrate, and hours worked: " << endl;
 cin >> id >> payRate >> hours;

 //Loop to check for valid numbers.
   while ((id < 0) || payRate < 0 || hours < 0)
  {
   cout << "Please enter valid values (Positive numbers): " << endl;
   cin >> id >> payRate >> hours;
  }
}
void getEmployeeInfo()
{
    std::cout << "Please enter your employee id, pay rate, and hours worked:\n";
    std::cin >> id >> payRate >> hours;

    //Loop to check for valid numbers.
    while (id < 0 || payRate < 0 || hours < 0)
    {
        std::cout << "Please enter valid values (Positive numbers):\n";
        std::cin >> id >> payRate >> hours;
    }
}

Context

StackExchange Code Review Q#81854, answer score: 10

Revisions (0)

No revisions yet.