Hello, I'm new to programming and I have to write a program for a retail store. The problem I'm having is when I repeat my loop, my "total" does not revert back to zero, so it will increment the previous total from the first loop iteration which essentially shows my output is incorrect, can anyone help me out with that. I don't know if I should flush something, but keep in mind that since I'm a beginner the class won't let us use complicated functions.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double cashDrawer = 500.00;
int productID = 0;
int quantity = 0;
double cashTendered = 0.0;
double fCash;
double mReceived = 0.0;
double price = 0.0;
double subtotal = 0.0;
double salesTax = 0.075;
double total = 0.0;
char anotherSale = 'x';
do // Loop for repeat sales
{
do
{
cout << "Please enter desired Product ID or -1 to exit: "; // Enter the first Product ID for the first sale (-1 to exit)
// Main loop for each sale
cin >> productID;
if (productID == -1)
break;
cout << "Please enter desired quantity: ";
cin >> quantity;
switch (productID)
{
case 101:
price = 65.00;
subtotal = ((price * salesTax) + price) * quantity;
break;
case 102:
price = 12.50;
subtotal = price * quantity;
break;
case 103:
price = 24.50;
subtotal = price * quantity;
break;
case 104:
price = 38.75;
subtotal = ((price * salesTax) + price) * quantity;
break;
case 105:
price = 17.80;
subtotal = ((price * salesTax) + price) * quantity; // Switch statement to determine the price, and calculate sales tax, if any, for the item.
break;
case 106:
price = 16.50;
subtotal = price * quantity;
break;
case 107:
price = 42.85;
subtotal = ((price * salesTax) + price) * quantity;
break;
case 108:
price = 32.99;
subtotal = ((price * salesTax) + price) * quantity;
break;
case 109:
price = 28.75;
subtotal = ((price * salesTax) + price) * quantity;
break;
case 110:
price = 51.55;
subtotal = price * quantity;
break;
default:
subtotal = 0.00;
}
total += subtotal;
}while (productID != -1);
cout << "Total due for purchase: " << total << endl; // Print properly formatted output for each sale
cout <<"How much cash is tendered? ";
cin >> cashTendered;
mReceived = cashTendered - total;
fCash = cashDrawer + cashTendered - mReceived;
cout << "The cash left in the drawer: " << fCash << endl; // Display how much is in the cash drawer at the end
cout << "Do you want another sale (Y for yes and N for no)? "; // Get Next Product ID
cin >> anotherSale;
}while (anotherSale != 'N');
}