Using functions

I need to write a program that processes payroll for employees given. The program must read employee name, salary, taxes (which must be calculated based on income: 0% for salaries under $1000, 10% for salaries $1000-$2000, and 15% for salaries above $2000), gross pay (or salary after taxes) and output that to an output file and display it in a summary table. I also am required to use the functions I have written out in the prototypes.

Here is the input file information we were given:
5000.67 George Washington
3000.99 John Adams
1000.00 Thomas Jefferson
500.89 Abraham Lincoln
1500.00 Jimmy Carter
1750.45 Ronald Regan
2300.34 Barack Obama
750.23 George W. Bush
2923.09 Richard Nixon
1567.11 John Kennedy

Here's what I have gotten so far, but I am new to C++ and really do not understand functions. Am I even going in the right direction? What do I do from here? Any help would be much appreciated! Thank you!


#include <iostream>
#include <iomanip>

using namespace std;

//function prototypes
double calculate_taxes(double salary);
double output_salary(std:: ofstream& out_stream, char name, double salary, double taxes, int count);
void output_summary(std:: ofstream& out_stream, double total_salaries, double total_taxes);

int main()
{
//variable declaration
int count;
double salary;
double taxes;
double gross_pay;
double total_salaries;
double total_taxes;
double total_gross_pay;
char junk;
string name;
ifstream in_stream;
ofstream out_stream;

//take input from the file Payroll_Info
ifstream in_stream;
in_stream.open("Payroll_Info.txt");
//error message if the input file cannot be accessed
if(in_stream.fail())
{
exit(1);
}
//continue reading input file until the end of the file, record the information
while(! in_steam.eof())
{
in_stream >> salary;
in_stream >> junk;
getline(in_stream, name);
count++;
output_salary(name, salary, taxes, count);
}
//close the files
in_stream.close();
out_stream.close();


return 0;
}

//calculate the taxes depending on salary amounts
double output_salary(std:: ofstream& out_stream, string name, double salary, double taxes, int count);
if(salary < 1000)
{
taxes = 0;
return salary;
}
else if(salary >= 1000 && salary < 2000)
{
double calculate_taxes(double salary);
{
double calculate_taxes = salary * 0.10;
return calculate_taxes;
}
}
else
{
double calculate_taxes(double salary);
{
double calculate_taxes = salary * 0.15;
return calculate_taxes;
}
}
//display the information in an organized table
void output_summary(std:: ofstream& out_stream, double total_salaries, double total_taxes);
{
cout << "# Name Salary Taxes Gross Pay " << endl;
cout << "--------------------------------------------------------------------" << endl;
cout << count << setw(3) << name << setw(20) << "$" << salary << setw(15) << "$" << taxes << setw(15) << "$" << gross_pay << setw(15) << endl;
cout << "====================================================================" << endl;
cout << "Total: " << total_salaries << setw(15) << total_taxes << setw(15) << total_gross_pay << setw (15) << endl;

}
So we have an idea, do you have a specific problem with the code(i.e. not reading the file, wrong output data, program crashing etc.)?
Here's what I have gotten so far, but I am new to C++ and really do not understand functions. Am I even going in the right direction? What do I do from here?


The first thing you need to do is to get organized.

Program Requirements:
The program must read employee name.
The program must show salary.
The program must show taxes.
The program must must be calculated based on income:
A. 0% for salaries under $1000.
B. 10% for salaries $1000-$2000.
C. 15% for salaries above $2000.
The program must show Net pay (or salary after taxes) (Gross is before taxes)
The program must write output to an output file.

The program must display it in a summary table.
The program must use functions.

Alright. That's better. Each line I've just written can be written as a function, so we've already got the last requirement licked. Now go right down the list. Write a program that inputs your data, and can display Name and Salary. There are a few ways to do this. You can read a text file into a class, into an array, a vector... That's your choice.
Once you have that done, display them. Then the fun part. Move that routine out of main, and you'll have your first function. When you get that working, move right on down the list...

Topic archived. No new replies allowed.