Sample Solution to Lab #2
//////////////////////////////////////////////////////////////////////////////// // Student name: Some Student // Student ID: 123-45-6789 // Course: CS161 // Assignment: #2 // Due date: 7-15-97 // // Description: program2.cpp // This program calculates the total amount the customer // of a carpet store should be charged. The calculation // is based on the square yards purhased, cost per square // yard, labor cost, floor preparation, volume discounts, // and tax. The program takes input from the user and results // are output to the screen in table format. //////////////////////////////////////////////////////////////////////////////// #include <iostream.h> #include <iomanip.h>
const float TAX_RATE = 0.04f; // global tax rate
// Function declarations(prototypes)
////////////////////////////////////////////////////////////////////////////////
// Function: doCustomer
// This function processes a
// customer using other functions
//
// Inputs: an integer representing the customer (value)
//
// Outputs: a float representing the customer charge (reference)
//
float doCustomer(int customer);
////////////////////////////////////////////////////////////////////////////////
// Function: getData
// This function prompts the user for data
// and returns the user input to calling
// function
// Inputs: an integer representing the customer (value)
//
// Outputs: a float representing square yards purchased (reference)
// a float representing cost per square yard (reference)
// a float representing labor per square yard (reference)
// a float representing floor prep cost (reference)
// a float representing discount rate (reference)
//
void getData(int customer,
int &sqr_yrds,
float &cost_per_sqr_yrd,
float &lbr_per_sqr_yrd,
float &prep_cost,
float &discount_rate);
////////////////////////////////////////////////////////////////////////////////
// Function: calculateCosts
// Calculates customer's costs based on the data input
// by the user
//
// Inputs: a float representing square yards purchased (value)
// a float representing cost per square yard (value)
// a float representing labor per square yard (value)
// a float representing floor preparation cost (value)
// a float representing discount rate (value)
//
// Outputs: a float representing carpet cost (reference)
// a float representing labor cost (reference)
// a float representing total discount (reference)
// a float representing total tax (reference)
// a float representing total customer charge (reference)
void calculateCosts(int sqr_yrds,
float cost_per_sqr_yrd,
float lbr_per_sqr_yrd,
float prep_cost,
float discount_rate,
float &carpet_cost,
float &labor_cost,
float &discount,
float &total_tax,
float &customer_charge);
////////////////////////////////////////////////////////////////////////////////
// Function: printResults
// This function prints the results of each user
//
// Inputs: an integer representing the customer (value)
// a float representing square yards
// a float representing cost per square yard
// a float representing labor per square yard
// a float representing floor preparation cost
// a float representing total carpet cost per customer
// a float representing total labor cost per customer
// a float representing total discount per customer
// a float representing total tax per customer
// a float representing total charge per customer
void printResults(int customer,
int sqr_yrds,
float cost_per_sqr_yrd,
float lbr_per_sqr_yrd,
float prep_cost,
float carpet_cost,
float labor_cost,
float discount,
float total_tax,
float customer_charge);
void main(void)
{
float charge1, // Total charge per customer 1-4
charge2,
charge3,
charge4;
float grandTotal; // Sum of all customers(1-4) total charge
//Call function doCustomer for each of 4 customers and returns
//total charge for each customer to charge#
charge1 = doCustomer(1);
charge2 = doCustomer(2);
charge3 = doCustomer(3);
charge4 = doCustomer(4);
//Calculates grand total for all customers
grandTotal = charge1 + charge2 + charge3 + charge4;
//Outputs grand total to screen
cout << setw(23) << "Total for all customers: " << setw(5)
<< "$" << setw(10) << grandTotal << endl;
}
//Function descriptions
float doCustomer(int customer)
{
float charge; // Total charge for customer
int sqr_yrds; // Square yards purchased
float cost_per_sqr_yrd; // Cost per square yard
float lbr_per_sqr_yrd; // Labor per square yard
float prep_cost; // Floor preparation cost
float discount_rate; // Discount rate applied to carpet cost
float carpet_cost; // Total cost for carpet
float labor_cost; // Total cost for labor
float discount; // Total discount for customer
float total_tax; // Total tax on purchase
//Gets customer data from user
getData(customer, sqr_yrds, cost_per_sqr_yrd, lbr_per_sqr_yrd,
prep_cost, discount_rate);
//Calculate the cost for the customer
calculateCosts(sqr_yrds, cost_per_sqr_yrd, lbr_per_sqr_yrd,
prep_cost, discount_rate, carpet_cost, labor_cost,discount, total_tax, charge);
//Print the results of calculations for the customer
printResults(customer, sqr_yrds, cost_per_sqr_yrd, lbr_per_sqr_yrd,
prep_cost, carpet_cost, labor_cost, discount,
total_tax, charge);
//Return the calculated charge to calling function for grand totalcalc
return charge;
}
void getData(int customer, int &sqr_yrds, float &cost_per_sqr_yrd,
float &lbr_per_sqr_yrd, float &prep_cost, float &discount_rate)
{
//Inform user which customer to input data for
cout << "Accepting data for customer " << customer << endl;
//Prompt user for each data value and store in appropriate variable
cout << setw(37) << "Input square yards purchased: ";
cin >> sqr_yrds;
cout << setw(37) << "Input cost per square yard: "; cin >> cost_per_sqr_yrd;
cout << setw(37) << "Input labor cost per square yard: "; cin >> lbr_per_sqr_yrd;
cout << setw(37) << "Input floor preparation cost: "; cin >> prep_cost;
cout << setw(36) << "Input discount rate for transaction: ";
cin >> discount_rate;
//Provides spacing of output for readability
cout << endl;
}
void calculateCosts(int sqr_yrds, float cost_per_sqr_yrd,
float lbr_per_sqr_yrd, float prep_cost,
float discount_rate, float &carpet_cost,
float &labor_cost, float &discount, float &total_tax,
float &customer_charge)
{
//Calculate carpet cost
carpet_cost = sqr_yrds * cost_per_sqr_yrd;
//Calculate labor cost
labor_cost = sqr_yrds * lbr_per_sqr_yrd + prep_cost;
//Calculate total discount
discount = carpet_cost * discount_rate;
//Calculate total tax on carpet NOTE: tax is on carpet cost afterdiscount
total_tax = (carpet_cost - discount) * TAX_RATE;
//Calculate total customer charge
customer_charge = carpet_cost + labor_cost - discount + total_tax;
}
void printResults(int customer, int sqr_yrds, float cost_per_sqr_yrd,
float lbr_per_sqr_yrd, float prep_cost, float carpet_cost,
float labor_cost, float discount, float total_tax,
float customer_charge)
{
//Prints results of user input and calculations
cout << setw(30) << "Results for customer " << customer << endl;
cout << setw(31) << "----------------------" << endl;
//Sets floating point output as fixed-point mode
cout.setf(ios::fixed, ios::floatfield);
//Sets floating point output to always use decimal point
cout.setf(ios::showpoint);
//Sets the precision of floating point output to two decimal places
cout << setprecision(2);
// output all input values and calculated values
cout << setw(24) << "Square yards purchased:" << setw(6) << " "
<< setw(10) << sqr_yrds << endl;
cout << setw(24) << "Cost per square yard:" << setw(6) << "$"
<< setw(10) << cost_per_sqr_yrd << endl;
cout << setw(24) << "Labor per square yard:" << setw(6) << "$"
<< setw(10) << lbr_per_sqr_yrd << endl;
cout << setw(24) << "Floor preparation cost:" << setw(6) << "$"
<< setw(10) << prep_cost << endl;
cout << setw(24) << "Cost for carpet:" << setw(6) << "$"
<< setw(10) << carpet_cost << endl;
cout << setw(24) << "Cost for labor:" << setw(6) << "$"
<< setw(10) << labor_cost << endl;
cout << setw(24) << "Discount on carpet:" << setw(6) << "$"
<< setw(10) << discount << endl;
cout << setw(24) << "Tax on carpet:" << setw(6) << "$"
<< setw(10) << total_tax << endl;
cout << setw(24) << "Charge to customer:" << setw(6) << "$"
<< setw(10) << customer_charge << endl;
//Prints a boundary between customers
cout << "----------------------------------------" << endl;
//Provides spacing of output for readability
cout << endl;
}