Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How to get C++ program to do correct output? PROGRAM : #include #include using n

ID: 3902269 • Letter: H

Question

How to get C++ program to do correct output?

PROGRAM :

#include
#include
using namespace std;

int main()
{
double cashDrawer = 500.00;
int productID = 0;
int quantity = 0;
double price = 0.0;
double subtotal = 0.0;
double salesTax = 0.0;
double totalSale = 0.0;
int anotherSale = 1;
double taxrate = 0;

// Loop for repeat sales
while (anotherSale == 1)
{

subtotal=0;


// Enter the first Product ID for the first sale (-1 to exit)

cout << "Enter the first product ID (-1 to exit): ";
cin >> productID;
  
if(productID!=-1)
{
cout << "Enter quantity of item purchased: ";
cin >> quantity;
}


// Main loop for each sale
while (productID != -1)
{

// Switch statement to determine the price, and calculate sales tax,
switch(productID)
{
case 101:
price = 65.0;
taxrate = 0.075;
break;
case 102:
price = 12.50;
taxrate = 0.0;
break;

case 103:
price = 24.50;
taxrate = 0.0;
break;

case 104:
price = 38.75;
taxrate = 0.075;
break;

case 105:
price = 17.80;
taxrate = 0.075;
break;

case 106:
price = 16.50;
taxrate = 0.0;
break;

case 107:
price = 42.85;
taxrate = 0.075;
break;

case 108:
price = 32.99;
taxrate = 0.075;
break;

case 109:
price = 28.75;
taxrate = 0.075;
break;

case 110:
price = 51.55;
taxrate = 0.0;
break;
}
  
  
  
  
subtotal += quantity * price;
salesTax = (quantity * price) * taxrate;

// Get next Product ID
cout << "Enter the next product ID: ";
cin >> productID;
  
if(productID!=-1)
{
cout << "Enter quantity of product: ";
cin >> quantity;
}
  
}

// Print properly formatted output for each sale
cout << "The total sale for this transaction is: " << subtotal << endl;
totalSale = subtotal + salesTax;

cashDrawer+= totalSale;


// Another sale?
cout<<"if there is another sale press 1 to continue OR 0 to end: ";
cin>>anotherSale;

}

// Display how much is in the cash drawer at the end

cout << "The balance in the cash drawer is $" << cashDrawer << endl;


}

Transaction 1:

Transaction 2:

Each transaction is supposed to have its OWN total not added. The program correctly outputs the total of each sale BUT outputs incorrect total in cash drawer. output below, please help.

OUTPUT:

Enter the first product ID (-1 to exit): 101
Enter quantity of item purchased: 2
Enter the next product ID: 102
Enter quantity of product: 1
Enter the next product ID: -1
The total sale for this transaction is: 142.5
if there is another sale press 1 to continue OR 0 to end: 1  
Enter the first product ID (-1 to exit): 101
Enter quantity of item purchased: 2
Enter the next product ID: -1
The total sale for this transaction is: 130
if there is another sale press 1 to continue OR 0 to end: 0
The balance in the cash drawer is $782.25

Product ID 101 102 103 104 105 106 107 108 109 110 Price $65.00 12.50 $24.50 $38.75 $17.80 $16.50 $42.85 $32.99 $28.75 $51.55 Quantity Taxable Yes No No Yes Yes No Yes Yes Yes No

Explanation / Answer

There is a couple of reasons why your program is giving incorrect values for cash drawer. First of all, after each sale, the cost is displayed without adding the tax rate, once displayed, the tax rate is added and appended the total to cash drawer, so as a result, the user couldn’t see the price including tax (until cash drawer is displayed at the end). So I changed the code, after each sale, the cost and corresponding sales tax is added and then displayed. Also, there is one more thing I noticed. The cash drawer’s initial value is given as 500. If that’s intentional, then there’s no problem, the issues must be fixed by now.

//corrected code

#include<iostream>

using namespace std;

int main()

{

double cashDrawer = 500.00;

int productID = 0;

int quantity = 0;

double price = 0.0;

double subtotal = 0.0;

double salesTax = 0.0;

double totalSale = 0.0;

int anotherSale = 1;

double taxrate = 0;

// Loop for repeat sales

while (anotherSale == 1)

{

subtotal=0; //resetting the total

salesTax=0; //resetting the tax

// Enter the first Product ID for the first sale (-1 to exit)

cout << "Enter the first product ID (-1 to exit): ";

cin >> productID;

if(productID!=-1)

{

cout << "Enter quantity of item purchased: ";

cin >> quantity;

}

// Main loop for each sale

while (productID != -1)

{

// Switch statement to determine the price, and calculate sales tax,

switch(productID)

{

case 101:

price = 65.0;

taxrate = 0.075;

break;

case 102:

price = 12.50;

taxrate = 0.0;

break;

case 103:

price = 24.50;

taxrate = 0.0;

break;

case 104:

price = 38.75;

taxrate = 0.075;

break;

case 105:

price = 17.80;

taxrate = 0.075;

break;

case 106:

price = 16.50;

taxrate = 0.0;

break;

case 107:

price = 42.85;

taxrate = 0.075;

break;

case 108:

price = 32.99;

taxrate = 0.075;

break;

case 109:

price = 28.75;

taxrate = 0.075;

break;

case 110:

price = 51.55;

taxrate = 0.0;

break;

}

subtotal += (double)quantity * price;

//adding to to total tax for this sale

salesTax += (double)(quantity * price) * taxrate;

// Get next Product ID

cout << "Enter the next product ID: ";

cin >> productID;

if(productID!=-1)

{

cout << "Enter quantity of product: ";

cin >> quantity;

}

}

totalSale = subtotal + salesTax; //adding the tax for current sale

/*

* you were displaying the cost without adding the tax first, and once you displayed

* the cost, you add tax and then update the cashdrawer, which was causing incorrect

* values. Fixed it

*/

// Print properly formatted output for each sale

cout << "The total sale for this transaction is: " << totalSale << endl;

cashDrawer+= totalSale;

// Another sale?

cout<<"if there is another sale press 1 to continue OR 0 to end: ";

cin>>anotherSale;

}

// Display how much is in the cash drawer at the end

cout << "The balance in the cash drawer is $" << cashDrawer << endl;

}

/*OUTPUT*/

Enter the first product ID (-1 to exit): 101

Enter quantity of item purchased: 1

Enter the next product ID: 102

Enter quantity of product: 2

Enter the next product ID: -1

The total sale for this transaction is: 94.875

if there is another sale press 1 to continue OR 0 to end: 1

Enter the first product ID (-1 to exit): 104

Enter quantity of item purchased: 3

Enter the next product ID: -1

The total sale for this transaction is: 124.969

if there is another sale press 1 to continue OR 0 to end: 0

The balance in the cash drawer is $719.844

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote