jeudi 25 juin 2020

How to repeat the whole process for another customer until user stop the program?

When the user enters is prompt with the "Continue with another customer (Y/N)?" They should be asked to press Y to repeat the program with a different customer, or N to end the program entirely. I'm not sure where to put the do-while statement in the code for. I have tried to put if statments, that doesnt work. When doing the do while statements the "Welcome to pizza world" text comes up, but it does not repeat the code, it will just end the program normally.

    #include <iostream>
using namespace std;
int main() {
    int pizzas, drinks;
    char drink_type, more;
    double order = 0.0;
    int count = 0, total_sodas = 0, total_milkshake = 0, total_pizzas = 0;
    //This will set the precision of the decimal output 2 decimal places.
    cout.setf(ios::showpoint);
    cout.precision(2);
    cout.setf(ios::fixed);
    cout << "Welcome to Pizza World" << endl;
    do{
        count++;
        cout << "\n\nHow many pizzas would you like?";
        cin >> pizzas;
        while (pizzas < 0)
        {
            cout << "That's an invalid number of pizzas.Try again:";
            cin >> pizzas;
        }
        cout << "How many drinks would you like?";
        cin >> drinks;
        while (drinks < 0)
        {
            cout << "That's an invalid number of drinks.Try again:";
            cin >> drinks;
        }
        if (drinks > 0) {
            cout << "You chose to order " << drinks << " drinks. Enter 'S' for soda and 'M' for milkshake:";
            cin >> drink_type;
            switch (drink_type)
            {
            case 'S':
            case 's':
                order = order + 1.95 * drinks;
                total_sodas = total_sodas + drinks;
                break;
            case 'M':
            case 'm':
                order = order + 4.25 * drinks;
                total_milkshake = total_milkshake + drinks;
                break;
            }
        }
        order = order + 12.49 * pizzas;
        total_pizzas = total_pizzas + pizzas;
        order = order + 0.06 * order;
        cout << "\n\nThe current order total including tax is $" << order << endl << endl;
        cout << "Would you like to place another order(Y/N)?";
        cin >> more;
        // Resetting the order.
        order = 0.0;
    }while (more == 'Y' || more == 'y');
        double subtotal = 0.0, tax;
        cout << "\n##########################" << endl;
        cout << "Your itemized bill for " << count << " orders:" << endl;
        // Showing the calculations of every item only if its count is more than zero
        if (total_pizzas > 0)
        {
            subtotal = subtotal + total_pizzas * 12.49;
            cout << "Pizzas: " << total_pizzas << " x $12.49 = $" << total_pizzas * 12.49 << endl;
        }
        if (total_sodas > 0)
        {
            subtotal = subtotal + total_sodas * 1.95;
            cout << "Sodas: " << total_sodas << " x $1.95 = $" << total_sodas * 1.95 << endl;
        }
        if (total_milkshake > 0)
        {
            subtotal = subtotal + total_milkshake * 4.25;
            cout << "Sodas: " << total_milkshake << " x $4.25 = $" << total_milkshake * 4.25 << endl;
        }
        cout << "-------------------------------" << endl;
        cout << "Subtotal = $" << subtotal << endl;
        tax = subtotal * 0.06;
        cout << "Tax = $" << tax << endl;
        cout << "Total = $" << (subtotal + tax) << endl;
        do {
            cout << "Continue with another customer (Y/N)? \n";
            cin >> more;
            cout << "Welcome to Pizza World" << endl;
            if (more == 'Y' || more == 'y')break;
        } while (1);
        system("pause");
        return 0;
}

Aucun commentaire:

Enregistrer un commentaire