Hi, can take a look at this program, I just have a question, instead of using namespace std, what can I replace it with, but also have a working program?
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std; //////////////////////////////////// namespace std is here
#define PARKING_DEDUCTION 10.00 // The fee deducted for parking
#define TRANSIT_DEDUCTION 5.00
#define REGULAR_HRS_MAX 40.00 // The max amount of regular hours
#define MIN_HRS 0.00 // The min amount of total hours
#define MAX_HRS 60.00 // The max amount of total hours
#define MIN_PAY 0.00 // The min amount of hourly pay
#define MAX_PAY 99.99 // The max amount of hourly pay
#define TAX_RATE .30
#define OVERTIME_RATE 1.50
#define SIZE 10 // size of array
void input_data(string &full_name, double &hours, double &hourly_rate, double &deductions);
string join_name(string first_name, string last_name);
void split_hours(double hours, double ®ular_hours, double &overtime_hours);
double cal_gross_pay(double regular_hours, double overtime_hours, double hourly_rate);
double cal_tax(double gross_pay, double tax_rate);
double cal_net(double gross_pay, double tax, double deductions);
void output_payroll_data(std::ostream &stream, string full_name, double regular_hours, double overtime_hours, double hourly_rate,
double gross_pay, double tax, double deductions, double net_pay);
char get_yesno(string question);
double calculate_deductions();
double input_bounded_value(string description, double min_value, double max_value);
void summary(ofstream &outfile, int run, double total_gross);
double calc_sum(double gross[], int run);
double calc_min(double gross[], int run);
double calc_max(double gross[], int run);
void payroll_summary(ofstream &outfile, int run, string names[], double gross[], double total_gross, double avg_gross,
double min_gross, double max_gross);
int main()
{
string name[SIZE],
full_name;
double hours;
double overtime_hours;
double regular_hours;
double hourly_rate;
double gross_pay;
double tax;
double net_pay;
double deductions;
double total_gross; // The total gross paid to all employees
double gross[SIZE]; // Array for gross pay
double avg_gross; // Average of the gross pay
double min_gross; // Lowest gross pay
double max_gross; // Highest gross pay
double reg_hrs[SIZE];
double ovt_hrs[SIZE];
double hourlyrate[SIZE];
double taxes[SIZE];
double deduct[SIZE];
double netpay[SIZE];
std::ofstream outfile; //////////// output stream for writing to a fle
int run;
outfile.open("c:\\temp\\payroll.txt");
if (!outfile.is_open())
{
cout << "Cannot open report file" << endl;
exit(EXIT_FAILURE);
}
run = 0;
total_gross = 0;
do
{
input_data(full_name, hours, hourly_rate, deductions);
split_hours(hours, regular_hours, overtime_hours);
gross_pay = cal_gross_pay(regular_hours, overtime_hours, hourly_rate);
tax = cal_tax(gross_pay, TAX_RATE);
net_pay = cal_net(gross_pay, tax, deductions);
output_payroll_data(std::cout, full_name, regular_hours, overtime_hours, hourly_rate, gross_pay,
tax, deductions, net_pay);
output_payroll_data(outfile, full_name, regular_hours, overtime_hours, hourly_rate, gross_pay,
tax, deductions, net_pay);
name[run] = full_name;
gross[run] = gross_pay;
reg_hrs[run] = regular_hours;
ovt_hrs[run] = overtime_hours;
hourlyrate[run] = hourly_rate;
taxes[run] = tax;
deduct[run] = deductions;
netpay[run] = net_pay;
run = run + 1;
total_gross = total_gross + gross_pay;
} while (run < SIZE && get_yesno("Process another employee (Y/N)? ") == 'Y');
// Summary
summary(outfile, run, total_gross);
if (run > 0)
{
total_gross = calc_sum(gross, run);
avg_gross = total_gross / run;
min_gross = calc_min(gross, run);
max_gross = calc_max(gross, run);
}
else
{
total_gross = 0.00;
avg_gross = 0.00;
min_gross = 0.00;
max_gross = 0.00;
}
payroll_summary(outfile, run, names, gross, total_gross, avg_gross, min_gross, max_gross);
}
void input_data(std::istream &stream, string &full_name, double &hours, double &hourly_rate, double &deductions)
{
string first_name;
string last_name;
cout << " Enter employee's first name: ";
cin >> first_name;
cout << " Enter last name of employee : ";
cin >> last_name;
full_name = join_name(first_name, last_name);
deductions = calculate_deductions();
hours = input_bounded_value(" number of hours worked ", MIN_HRS, MAX_HRS);
hourly_rate = input_bounded_value(" hourly pay rate ", MIN_PAY, MAX_PAY);
}
// A processing module that concatenates an employee's first and last names to a full
// name in the form "Last, First".
string join_name(string first_name, string last_name)
{
string full_name; // The employee's full name
full_name = last_name + ", " + first_name;
return full_name;
}
// An input module that reads a value from the user and validates that it is within a specific range.
double input_bounded_value(string description, double min_value, double max_value)
{
double value; // Value entered by user
do
{
cout << "Enter " << description << " : ";
cin >> value;
if (value > max_value || value < min_value)
{
cout << "The " << description << " must be between " << min_value << " and " << max_value;
cout << endl;
cout << endl;
}
} while (value > max_value || value < min_value);
return value;
}
// A processing module that calculates the parking deductions for an employee
double calculate_deductions()
{
double deductions;
if (get_yesno("Does the employee us the parking garage (Y/N)?: ") == 'Y')
{
deductions = PARKING_DEDUCTION;
}
else if (get_yesno("Does the employee participate in the transit program (Y/N)?: ") == 'Y')
{
deductions = TRANSIT_DEDUCTION;
}
else
{
deductions = 0.00;
}
return deductions;
}
// A processing module that splits the total number of hours worked by an employee
// into regular and overtime hours.
void split_hours(double hours, double ®ular_hours, double &overtime_hours)
{
if (hours > REGULAR_HRS_MAX)
{
overtime_hours = hours - REGULAR_HRS_MAX;
}
else overtime_hours = 0;
regular_hours = hours - overtime_hours;
}
// A processing module that calculates an employee's gross pay
double cal_gross_pay(double regular_hours, double overtime_hours, double hourly_rate)
{
double gross_pay;
gross_pay = (regular_hours * hourly_rate) + (overtime_hours * hourly_rate * OVERTIME_RATE);
return gross_pay;
}
// A processing module that calculates the tax on an employee's gross pay
double cal_tax(double gross_pay, double tax_rate)
{
double tax;
tax = gross_pay * tax_rate;
return tax;
}
// A processing module that calculates an employee's net pay, given gross pay, taxes, and deductions.
double cal_net(double gross_pay, double tax, double deductions)
{
double net_pay; // The employee's net pay
net_pay = gross_pay - tax - deductions;
return net_pay;
}
// An output module that displays formatted payroll data .
void output_payroll_data(std::ostream &stream, string full_name, double regular_hours, double overtime_hours, double hourly_rate,
double gross_pay, double tax, double deductions, double net_pay)
{
stream << std::fixed << std::setprecision(1);
stream << endl;
stream << "12345678901234567890##21.00##21.00##321.00##4321.00##321.00##321.00##4321.00" << endl;
stream << " Reg. Ovt. Hourly Gross Net " << endl;
stream << "Name Hours Hours Rate Pay Taxes Deduct Pay " << endl;
stream << "==================== ===== ===== ====== ======= ====== ====== =======" << endl;
stream << left << setw(20) << full_name << " ";
stream << fixed << setprecision(2);
stream << right << setw(5) << regular_hours << " ";
stream << setw(5) << overtime_hours << " ";
stream << setw(6) << hourly_rate << " ";
stream << setw(7) << gross_pay << " ";
stream << setw(6) << tax << " ";
stream << setw(6) << deductions << " ";
stream << setw(7) << net_pay << endl << endl;
}
char get_yesno(string question)
{
char answer; // Answer to the question
do
{
cout << question;
cin >> answer;
if (answer == 'y')
{
answer = 'Y';
}
else if (answer == 'n')
{
answer = 'N';
}
if (answer != 'Y' && answer != 'N')
{
cout << "Please type 'Y' for yes or 'N' for no" << endl << endl;
}
} while (answer != 'Y' && answer != 'N');
return answer;
}
void summary(ofstream &outfile, int run, double total_gross)
{
outfile << "Employees processed: " << run << endl;
outfile << "Total gross pay: $" << total_gross << endl << endl;
}
// A processing module that computes the sum of the grosses
//
double calc_sum(double gross[], int run)
{
double sum = 0.0; // Sum of grosses
int i; // Counter variable
for (i = 0; i < run; i++)
{
sum = sum + gross[i];
}
return sum;
}
// A processing modulae that finds the smallest number in an array.
double calc_min(double gross[], int run)
{
double minimum; // Minimum gross
int i; // Counter variable
minimum = gross[0];
for (i = 1; i < run; i++)
{
if (gross[i] < minimum)
{
minimum = gross[i];
}
}
return minimum;
}
// A processing module that finds the biggest number in an array.
double calc_max(double gross[], int run)
{
double maximum; // Maximum gross
int i; // Counter Vaiable
maximum = gross[0];
for (i = 1; i < run; i++)
{
if (gross[i] > maximum)
{
maximum = gross[i];
}
}
return maximum;
}
void payroll_summary(ofstream &outfile, int run, string names[], double gross[], double total_gross, double avg_gross,
double min_gross, double max_gross)
{
int i; // Counter variable
stream << "12345678901234567890##54321.00" << endl;
stream << "==============================" << endl;
stream << " Employee Payroll Summary " << endl;
stream << "==============================" << endl << endl;
stream << " Gross " << endl;
stream << "Name Pay " << endl;
stream << "==================== ========" << endl;
for (i = 0; i < run; i++)
{
stream << setw(20) << left << names[i] << " ";
stream << setw(8) << right << gross[i] << endl;
}
stream << endl;
stream << "Total gross pay : $" << setw(8) << total_gross << endl;
stream << "Average gross pay : $" << setw(8) << avg_gross << endl;
stream << "Minimum gross pay : $" << setw(8) << min_gross << endl;
stream << "Maximum gross pay : $" << setw(8) << max_gross << endl << endl;
outfile.close();
}
If you have any doubts, please give me comment...
The alternative to namespace std statement is to specify the namespace to which the identifier belongs using the scope operator(::) each time we declare a type.
Example:
generally print, read, etc... we use directly with cout, cin, etc... instead of that we will use std::cout, std::cin, etc... without using namespace std.
some examples:
left -> std::left
right -> std::right
setw -> std::setw
endl -> std::endl;
string -> std::string
ifstream -> std::ifstream
ofstream -> std::ofstream
Hi, can take a look at this program, I just have a question, instead of using...