1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
// Program id: CSLab05.cpp
// This is our template for CS Lab 05
// REFACTORING PRACTICE PROGRAM
// This program is used for, Department of Computer Science Laboratories
// Goal: refactor main into functions
// examine each portion in main and analyze the code for refactoring
// Next build your function prototype
// Next build your function by copying and pasting the code from main, into your function
// repeat until complete
#include<iostream>
#include <iomanip>
#include <cctype>
using namespace std; // forgive me for being lazy!
const double TAX_RATE = 0.05;
int getPrice();
double getPurchased();
char getSaleType();
double getSaleTotal(char, double, double);
void displayTotal(double, double, double, char);
int main()
{
double price, quant, total, saleType;
price = getPrice();
quant = getPurchased();
saleType = getSaleType();
total = getSaleTotal(saleType, price, quant);
displayTotal(price, quant, total, saleType);
return 0;
}
int getPrice()
{
double price;
cout << "Enter price $: ";
cin >> price;
while (price <= 0)
{
cout << "Enter a price more than 0 dollars and 0 cents. $: ";
cin >> price;
}
return price;
}
double getPurchased()
{
double quant;
cout << "Enter the quantity purchased: ";
cin >> quant;
while (quant < 1)
{
cout << "Quantity must be greater than one. Enter the quantity purchased: ";
cin >> quant;
}
return quant;
}
char getSaleType()
{
char saleType;
do
{
cout << "Type W if this is wholesale purchase. \n"
<< "Type R if this is retail purchase. \n"
<< "then press return... \n";
cin.ignore();
cin.get(saleType);
bool saleFlag = false;
if (!(saleType == 'W' && saleType == 'R'))
{
saleFlag = true;
}
} while (saleFalg); //here's the error
return saleType;
}
double getSaleTotal(char saleType, double price, double quant)
{
double subTotal, total;
if ((saleType == 'W') || (saleType == 'w'))
{
total = price * quant;
}
else if ((saleType == 'R') || (saleType == 'r'))
{
subTotal = price * quant;
total = subTotal + subTotal * TAX_RATE;
}
else
{
cout << "Error in the input...";
}
return total;
}
void displayTotal(double price, double quant, double total, char saleType )
{
cout << setprecision(2) << fixed << showpoint << quant << " items at $" << price << endl;
cout << "Total bill $" << total;
if ((saleType == 'R') || (saleType == 'r'))
{
cout << " includes sales tax.\n";
}
}
|