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');



}

Move lines 20 and 21 (beginning of the first do loop) up to just after line 6 so that all those variables are declared inside the first do loop. That way they will get reset to 0 each time the first do loop is executed.

maybe you didn't do it right

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    do  // Loop for repeat sales
    {
        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
        {
            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');



}

That didn't work either, it just gives me an error and if I try to run it, the loop stops

Yes, you need to move one line

int main()
{
        char anotherSale = 'x';
    do  // Loop for repeat sales
    {
        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;

Edited 3 Years Ago by Ancient Dragon

Ok, now I got it, so putting the variables within the first do loop will make it revert to 0, thank you for the help by the way, sorry I was being a bit frustrating.

A quick note about your code. If you use a multidimensional array you can avoid that whole switch block, which shortens your code considerably. Also there's one function you should become familiar with, round. When you're changing dollar amounts by percentages you want to round the result to 2 decimal places. The round function works well for this except that it only rounds to integer, thus you have to multiply by 100 round it then divide by 100 again. Kinda clunky but it works, and is fairly straight forward. Here's some code to study:

#include <iostream>
#include <iomanip>
#include <cmath>
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;

    const double salesTax = 0.075;

    const int maxprod = 10;
    double product[][2] =
    {
        {65.00, 1.0},
        {12.50, 0.0},
        {24.50, 0.0},
        {38.75, 1.0},
        {17.80, 1.0},
        {16.50, 0.0},
        {42.85, 1.0},
        {32.99, 1.0},
        {28.75, 1.0},
        {51.55, 0.0}
    };
    char anotherSale = 'x';
    do // Loop for repeat sales
    {
        double total = 0.0;

        do
        {
            double subtotal = 0.0;
            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;
            if (productID > 100 && productID <= 100 + maxprod)
            {
                int index = productID - 100;
                subtotal = (product[index][0] * quantity) + (product[index][1] * salesTax);
            }

            total += round(subtotal*100)/100;
            cout << "Total is: " << total << endl;
        }
        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');
}

One large advantage to using a structure like this, is that if you want to change thew product items, just change the array and adjust the maxprod constant accordingly. The rest of the code stays the same. Also if at some point you want to make the data more dynamic and use a text file or database, you code will need very little modifying, just fill the array and set a variable for the highest product index.

Edited 3 Years Ago by tinstaafl

This question has already been answered. Start a new discussion instead.