Program Output at Bottom of Page 1. You are given a starter program that you wil
ID: 3759971 • Letter: P
Question
Program Output at Bottom of Page
1. You are given a starter program that you will need to finish
2. You will then test this program two ways
First by letting the computer generate the data for input
Then, when that is done, by using the keyboard for data input
The following are some comments to help you complete the project
//GLOBAL DEFINITIONS...
const bool KEYBOARD = false; // false means - use random generator function rand() for keyboard input
ofstream Out("con"); // "con" means the "Out" stream is the console(i.e. screen)
//PROTOTYPE DEFINITIONS...
int getNumberOfSales(void);
int getItemNumber(void);
double getPrice(int itemNumber);
bool isTaxable(int itemNumber);
int getQuantity(int itemNumber);
double getCost(int itemNumber, int count, double price);
double getTax(double sales);
void printLine(ostream & w,int iNumber, int qty, double price,double cost, bool taxable);
void printTotal(ostream & w, int loopCount, double grandTotal, double taxDue);
void headings(ostream & w);
void startRandom(void);
void prepareOutput( ostream & w);
void main()
{
int differentItems, // Number of items to purchase-LOOPINGLIMIT
iNumber, // Item number code
qty; // Number of a particular item purchased
double price, // Price of a particular item
cost, // The cost of the item purchase = price * qty
taxableTotal, // Total of all taxable purchases
nonTaxableTotal, // Total of all nontaxable purchases
taxDue, // Tax due on the taxable total
grandTotal; // Sum of taxableTotal, nonTaxableTotal
bool taxable; // Flag to indicate if the item is taxable
// INSERT STATEMENTS-- Initialize three "total" accumulator variables
prepareOutput(Out); //establish xxxx.yy format for numeric values
if (!KEYBOARD) startRandom(); //set seed to 7750 if NOT using keyboard input
headings(Out); // COMMENT THIS OUT IF USING KEYBOARD INPUT
differentItems = getNumberOfSales(); // limit used in the for-loop below
for( int i = 0; i < differentItems ; i++)
{
iNumber = getItemNumber();
qty = getQuantity(iNumber);
price = getPrice(iNumber);
cost = getCost(iNumber, qty, price);
taxable = isTaxable(iNumber);
//headings(Out); //UNCOMMENT IF user input from keyboard
printLine(Out,iNumber,qty,price,cost,taxable);
// INSERT statements to accumulate costs for taxable and nontaxable totals
} //ENDFOR
// INSERT statement to calculate the grand total to be a combined taxable and nontaxable cost
taxDue = getTax( taxableTotal ) ;
printTotal( Out,differentItems, grandTotal, taxDue);
} //ENDMAIN
//------------- F U N C T I O N D E F I N I T I O N S ----------------------------------
void startRandom(void)
{
int seed;
cout<< "Enter seed value for random number generator: ";
cin>> seed; //INPUT 7750 SEED SO DURING TESTING PHASE WE CAN REPEAT SAME RESULTS
srand(seed);
}
void headings(ostream & w) //"w" parameter is equivalent to "Out" in main
{
w << setw(10)<<"item #" << setw(10) <<"quantity" << setw(10) <<"price"
<<setw(10)<<"cost"<<endl<<endl;
}
void prepareOutput(ostream & w) //"w" parameter is equivalent to "Out" in main
{
w << setioflags(ios::showpoint | ios::fixed) << setprecision(2);
}
//NOTICE THE IF STATEMENT and RAND function used below
//...YOU WILL NEED THEM IN OTHER FUNCTIONS BELOW
int getItemNumber(void)
{
int num;
if (KEYBOARD)
{ cout << "Enter item number(MUST BE 4-DIGIT): ";
cin>> num;
}
else
num = rand() % 9000 + 1000; //Num will be a random 4-digit number seeded with 7750
return num;
}
double getPrice(int num)
{
double price;
if (KEYBOARD)
{ cout << "Enter price for item(MUST BE BETWEEN .10 AND 10.09) " << num << " : ";
cin>> price;
}
else
price = double (rand() % 1000 + 10 ) / 100;
return price;
}
bool isTaxable(int itemNumber)
{
// INSERT LOGIC- USE IF to determine keyboard ... taxable y/n?
//versus setting taxable status only if itemNumber is divisible by 5
return true; //replace true with actual retunr value
}
int getQuantity(int num)
{
// INSERT LOGIC- USE IF to determine keyboard... input quantity
// versus setting input value via rand() fn for values between 1 and 8
return 1; //replace this with actual return value
}
int getNumberOfSales(void)
{
// INSERT LOGIC- USE IF to determine keyboard...input # of sales
// versus rand() fn setting value between 1 and 15
return ;
}
double getCost(int itemNumber, int count, double price)
{
// INSERT LOGIC - calculate cost to be count times price
//NOTE: For your solution the parameter itemNumber above is not needed
// i.e. If there was a table of itemNumbers that were on sale, then we might
// use the parameter itemNumber to search that table for discounts
return 10.0; //replace with actual return value
}
double getTax( double sales)
{
// INSERT LOGIC- calculate sales tax to be sales times the constant .0725
return 10.0; //replace with actual return value
}
void printLine(ostream & w,int iNumber, int qty, double price,double cost, bool taxable)
{
// INSERT LOGIC- look at sample program output to see what variables
// need to be output...for the asterisk - use the tertiary operator with "taxable"
w<< "***Detail line*******" << endl; // print a "*" for the item which is not taxable
}
----------USING RAND() FUNCTION FOR INPUT VALUES---------------------------------------
Enter seed value for random number generator: 7750
item # quantity price cost
3613 8 4.40 35.20
8655 2 6.22 12.44 *
4361 4 7.69 30.76
7460 7 1.91 13.37 *
5124 5 5.67 28.35
1967 2 5.37 10.74
1722 5 7.53 37.65
1396 3 8.45 25.35
1555 8 6.76 54.08 *
7401 7 2.65 18.55
4125 4 9.06 36.24 *
2800 5 4.22 21.10 *
Purchase: 323.83
Tax: 13.53
Amount: 337.36
12 kinds of different items purchased
* indicates item was not taxable
------------------------------------------------------------------------------------
-----------USING KEYBOARD TO INPUT VALUES--------------------------------
How many different items to purchase? 2
Enter item number: 1234
How many of item number 1234 were purchased?: 3
Enter price for item 1234 : 1.7
Is item number 1234 taxable ( y or n) : y
item # quantity price cost
1234 3 1.70 5.10
Enter item number: 1235
How many of item number 1235 were purchased?: 3
Enter price for item 1235 : 1.9
Is item number 1235 taxable ( y or n) : n
item # quantity price cost
1235 3 1.90 5.70 *
Purchase: 10.80
Tax: 0.37
Amount: 11.17
2 kinds of different items purchased
* indicates item was not taxable
STARTER SHELL:
Explanation / Answer
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<iomanip>
using namespace std;
const bool KEYBOARD = true;
ofstream Out("con");
int getNumberOfSales(void);
int getItemNumber(void);
double getPrice(int itemNumber);
bool isTaxable(int itemNumber);
int getQuantity(int itemNumber);
double getCost(int itemNumber, int count, double price);
double getTax(double sales);
void printLine(ostream & w,int iNumber, int qty, double price,double cost, bool taxable);
void printTotal(ostream & w, int loopCount, double grandTotal, double taxDue);
void headings(ostream & w);
void startRandom(void);
void prepareOutput( ostream & w);
int main()
{
int differentItems,iNumber,qty;
double price,cost,taxableTotal,nonTaxableTotal,taxDue,grandTotal;
bool taxable;
taxableTotal=0;
nonTaxableTotal=0;
grandTotal=0;
prepareOutput(Out);
if (!KEYBOARD) startRandom();
headings(Out);
differentItems = getNumberOfSales();
for( int i = 0; i < differentItems ; i++)
{
iNumber = getItemNumber();
qty = getQuantity(iNumber);
price = getPrice(iNumber);
cost = getCost(iNumber, qty, price);
taxable = isTaxable(iNumber);
// headings(Out);
printLine(Out,iNumber,qty,price,cost,taxable);
if(taxable)
taxableTotal+=cost;
else
nonTaxableTotal+=cost;
}
taxDue = getTax(taxableTotal);
grandTotal=taxableTotal+nonTaxableTotal;
printTotal( Out,differentItems, grandTotal, taxDue);
return 0;
}
void startRandom(void)
{
int seed;
cout << "Enter seed value for random number generator: ";
cin >> seed;
srand(seed);
}
int getItemNumber(void)
{
int num;
if (KEYBOARD)
{
cout << "Enter item number: ";
cin >> num;
}
else
num = rand() % 9000 + 1000;
return num;
}
double getPrice(int num)
{
double price;
if (KEYBOARD)
{
cout << "Enter price for item " << num << " : ";
cin >> price;
}
else
price = double (rand() % 1000 + 10 ) / 100;
return price;
}
bool isTaxable(int itemNumber)
{
char ans;
/*cout << " Is item number " << itemNumber << " taxable e(y or n ) : ";
cin >> ans;
if (ans == 'y')
return true;
else
return false;
if(itemNumber % 5 == 0)
return false;
else
return true;*/
if(KEYBOARD)
{
cout << " Is item number " << itemNumber << " taxable e(y or n ) : ";
cin >> ans;
if (ans == 'y')
return true;
else
return false;
}
else
{
if(itemNumber % 5 == 0)
return false;
else
return true;
}
}
int getQuantity(int num)
{
int ans;
if (KEYBOARD)
{
cout << "How many of item " << num << " were purchased ? :";
cin >> ans;
}
else
ans = rand() % 9;
return ans;
}
int getNumberOfSales(void)
{
int ans;
if (KEYBOARD)
{
cout << "How many different items to purchased? ";
cin >> ans;
}
else
ans = rand() % 16;
return ans;
}
double getCost(int itemNumber, int count, double price)
{
return count * price;
}
double getTax( double sales)
{
const double tax = 0.0725;
return sales * tax;
}
void printLine(ostream & w,int iNumber, int qty, double price,double cost, bool taxable)
{
cout <<setw(10)<< iNumber<<setw(10)<<qty<<setw(10)<<price<<setw(10)<<cost;
if (taxable == false)
cout << " *";
cout<<endl;
}
void printTotal( ostream & w,int loopCount, double grandTotal, double taxDue)
{
cout << "Purchase : "<<grandTotal<<endl<< "Tax : "<<taxDue<<endl <<"Amount : " << grandTotal + taxDue << endl;
cout << endl <<" * indicates item was not taxable ." << endl;
}
void headings(ostream & w)
{
cout << setw(10) << "item #" << setw(10) << "quantity" << setw(10) <<"price"<< setw(10) << "cost" << endl << endl;
}
void prepareOutput(ostream & w)
{
w << setiosflags(ios::showpoint | ios:: fixed) << setprecision(2);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.