payRoll system

I want to write a program enable the users perform which are adding, deleting employees, and/or displaying data of one or all employees, and computing total company payroll, Gross salary, net salary, calculating maximum and minimum salary. and my code couldn't run in right way what should i do!!


[#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
// User-defined Functions
void Adding_Empolyees (int, double, double);
void Deleting_Empolyees ();
void Displaying_Data (int);
//double Computing_Total_Company_Payroll ();
double CalcGross_Salary (double &payRate, double &hours);
double CalcTax(double& Gross_Salary, double &taxRate);
double CalcNet_Salary (double& Gross_Salary, double &Tax);
void printGrossSalary();
void printTable();
bool repeat();
void read();
// Defining terms for the program
int id,result;
double payRate, hours, salary, Tax, taxRate, Gross_Salary, Net_Salary;
ofstream outputFile;
ifstream inputFile;
string file, strings;
int main()
{

cout << "Please enter the name of the file you wish to save to: ";
cin >> file;
outputFile.open(file.c_str());

do {
// Get employee information
Adding_Empolyees ( id ,payRate, hours);
// Delete employee information
//void Deleting_Empolyees ();
//Display employee record
Displaying_Data (id);
// Calculate the total salary
Gross_Salary = CalcGross_Salary (payRate, hours);
cout<<Gross_Salary;
// Calculate the tax
cout << "Please enter the value of the taxRate ";
cin >> taxRate;
Tax = CalcTax(Gross_Salary, taxRate);
// Calculate the net salary
Net_Salary = CalcNet_Salary (Gross_Salary, Tax);
//For knowing the minimum salary
//cout << "The Floor for the Gross_Salary is " << floor (CalcGross_Salary ()) <<endl;
// For knowing the maximum salary
//cout << "The Ceil for Gross_Salary is " << ceil (CalcGross_Salary ()) <<endl;
// Prints the Table to the outputFile
printTable();
// Prints the wages to the output File
printGrossSalary();
} while(repeat());

// Close the outputfile
outputFile.close();
cout << file << " was closed succesfully" << endl;

read();

return 0;
}
// Function for asking user for Employee Id, pay rate, and the hours worked
void Adding_Empolyees (int id, double payRate, double hours )
{
/*cout<< "Please enter your employee id\n";
cin>> id ;
cout<< "Please enter your employee pay rate\n";
cin>> payRate;
cout<< "Please enter your employee hours worked:\n";
cin>> hours;
//Loop to check for valid numbers.
while (id < 0 || payRate < 0 || hours < 0)
{
cout<< "Please enter valid values (Positive numbers):\n";
cin>> id >> payRate >> hours;*/
id=52145 ; payRate= 150; hours=42;
}
// Function for deleting employees
/*void Deleting_Empolyees()
{
remove (file);
}
*/
// Function for display data
void Displaying_Data (int id)
{
cout<<id<<" "<<"\t"<<CalcNet_Salary <<CalcGross_Salary(payRate,hours)<<endl;
}

// Function for calculating the total salary
double CalcGross_Salary (double &payRate, double &hours)
{
return (Gross_Salary = payRate * hours);
}
// Function for calculating the tax
double CalcTax(double& Gross_Salary, double &taxRate)
{
return (Tax = Gross_Salary * taxRate);
}
// Function for calculating the net salary
double CalcNet_Salary (double& Gross_Salary, double &Tax)
{
return (Net_Salary = Gross_Salary - Tax);
}
// Function for printing information to the outputFile
void printGrossSalary()
{
outputFile << setw(10) << id
<< setprecision(2) << fixed << setw (10)
<< payRate
<< setprecision(2) << fixed << setw (10)
<< hours
<< setprecision(2) << fixed << setw (10)
<< Gross_Salary << endl;
}

// Function to print table to the outputFile
void printTable()

{
outputFile << setw(10) << "ID" << setw (10) << "Rate" << setw(10) << "Hours"
<< setw(10) << "Gross_Salary"<<endl;
outputFile << "---------------------------------------------------------\n";
}
// Function that asks the user if the process wants to be repeated
bool repeat()
{
char answer;
cout << "Would you like to continue entering values? (y/n): ";
cin >> answer;
return (answer == 'y' || answer == 'Y');
}

// Function that reads the data to the console
void read()
{
char repeatAnswer;
cout << "Would you like to read the file? (y/n): ";
cin >> repeatAnswer;
if (repeatAnswer == 'y' || repeatAnswer == 'Y')
{
inputFile.open(file.c_str());
while (!inputFile.eof())
{
getline(inputFile, strings);
cout << strings<< endl;
}
cout << "Reading " << file << " completed." << endl;
}

// Close inputFile
inputFile.close();
// Terminate the program
exit(0);
}




Last edited on
Hello MuhammadYoussef,

Welcome to the forum.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Your code would greatly benefit from the use of blank line to break up what you have and make it more readable.

It will take a little while to get your code to a point where I can read it, but I will work on it.

Andy
Hello andy,

thanks for your advice, i makes what you asked, i hope my post edit in right way and becoming more readable.
Hello MuhammadYoussef,

This may take awhile.

I will start at the top and work down.

The header files look OK for now.

Try to avoid using using namespace std; in your programs it may seem easy now, but WILL get you in trouble some day.

It is better to learn to qualify what is in the standard name space with "std::" and then to learn what is in the standard name space now while it is easy. And not all at once to try to keep a job.

What you are most likely to use for now is "std::cout", "std::cin" and "std::endl". About a week or so of typing this and you will not even notice that you are doing it.

The prototypes look good, but have problems I will explain shortly.

Try to avoid using everything as a global variable. This gives the whole file access to these variables and any function could change them. Sometimes when you do not expect. It is better to put these variables in main and pass them to the function. If a function needs to change the variable pass by reference.

Now for the prototypes, function calls and function definitions. All pass global variables to the functions and this creates a problem at the function definition. In the function definition these become local variables with the same name that over shadow the global variables. So when the function ends these local variables are destroyed and the global variables never get a value from the function. If you intend to use global variables you will need to remove what is between the () to use the global variables. This also means the prototypes and the function calls.

If you move the global variables to inside the main function then you can pass these variables to the function and is they need to change value you will need to pass them by reference.

The only global I would use is constexpr double TAXRATE{ 0.05 };. Or you could just "const" the capital letters will tell you that it is defined as a constant variable that can not be changed. This way you have only one place to change the tax rate if needed and it will be used everywhere in the program. Unless this rate varies then what you have done is correct.

In the "Adding_Empolyees" function you have a while loop which is a problem. First unless you enter a negative number you will never use the loop. Once inside the loop you give an error message / prompt, but the "cin" asks for input in a completely different way with out any example. A user will have no idea what to do. And there the point that you have no idea what was entered wrong. This while loop is better put after each "cin" to catch an error and deal with it then not later.

I only have worked down to the "Displaying_Data" function for now. The parameter of "id" does not need to be passed to the function because it is stored in a global variable the function has access to. The "cout" statement is wrong. First I believe that you wanted to call "CalcNet_Salary", but this is not a function call it is only being taken as variable. Second you can not calculate net salary until you calculate gross salary, so the order is backwards. Lastly the "CalcNet_Salary" functions needs a value for "Tax" which you do not have yet because yu have not called the "CalcTax"function yet. It looks like the only way to put a value in "Tax" is to call the function before the "cout" statement.

For now it looks like the program will only handle one employee at a time. In the future you will need to change this so the program can do more.

Hope that helps,

Andy

Topic archived. No new replies allowed.