How to get C++ program to do correct output? PROGRAM : #include <iostream> #incl
ID: 3902233 • Letter: H
Question
How to get C++ program to do correct output?
PROGRAM :
#include <iostream>
#include <iomanip>
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;
}
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
Explanation / Answer
The program generated correct, because The balance in the cash drawer is inclueded sales tax also. check the lines:
cout << "The total sale for this transaction is: " << subtotal << endl;
totalSale = subtotal + salesTax;
cashDrawer+= totalSale;
here we printing subtotal first, then calculating totalSale and we add to cashDrawer...
Here is solution:
#include <iostream>
#include <iomanip>
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
totalSale = subtotal + salesTax;
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.