Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add the prices in the purchase if I want to buy more items. But I do not know how to do that. For example if, I confirm my purchase and want to buy more items, I want it to add the prices that is confirmed to purchase, and if I finally do not want to buy more items and not to look for more items, that total price would be computed. The total is not keeping in track. For example is that, I confirmed to purchase the first item, and if I wanted to buy more, the price of the first item is not adding, while the 2nd item that I confirmed to purchase is the one that been showing.

What I have tried:

C++
int main()
    {
        int choice;
        int purchase;
        int quantity;
        double totalChoice1;
        double totalChoice2;
        char view;
        char confirm;
        char buyMore;
        char look;
        double alloy, apex, kraken, aorus;
        double oppo, alpha, rog, huawei;
        double ps4, nintendo, xbox, wii;

        alloy = 69.99;
        apex = 199;
        kraken = 90;
        aorus = 60;

do {
    cout << "What type of items would you like to view?" << endl;
    cout << " [1] Peripherals" << endl;
    cout << " [2] Mobile Phones" << endl;
    cout << " [3] Consoles" << endl;
    cout << " [4] Exit" << endl;
    cout << "Enter your choice: ";
    cin >> choice;


    if (choice == 1) {
        cout << "--------------------" << endl;
        cout << "What peripherals would you like to purchase?" << endl;
        cout << "[1] HyperX Alloy FPS PRO - $69.99" << endl;
        cout << "[2] SteelSeries APEX PRO - $199" << endl;
        cout << "[3] Razer Kraken X - $90" << endl;
        cout << "[4] AORUS K7 - $60" << endl;
        cout << "[5] BACK TO MENU"  << endl;
        cout << "Enter your choice: ";
        cin >> purchase;
        cout << "--------------------" << endl;


        if (purchase == 1) {
            cout << "How many would you like to purchase? ";
            cin >> quantity;
            totalChoice1 = quantity * alloy;

            cout << "The total price for that is " << totalChoice1 << endl;
            cout << "Confirm the Purchase? [Y]/[N]: ";
            cin >> confirm;

            if (confirm == 'Y') {
                totalChoice1; // This is just a trial code.
                cout << "Would you like to buy more items? [Y]/[N]: ";
                cin >> buyMore;
            }
            else if (confirm == 'N') {
                cout << "Do you still want to look for items? [Y]/[N]: ";
                cin >> look;

                if (look == 'N') {
                    break;
                }
            }
            else {
                break;
            }
        }
}
while (purchase == 5 || buyMore == 'Y' || look == 'Y');
cout << "The total price for your items is: " << totalChoice1; // This is also a trial code (totalChoice1)
}
Posted
Updated 14-Apr-20 21:37pm
v2

1 solution

The problem you have noticed is simple:
C++
totalChoice1 = quantity * alloy;
Each time you execute this, you discard all previous purchases.
Possibly, what you wanted was
C++
totalChoice1 = totalChoice1 + quantity * alloy;
Or
C++
totalChoice1 += quantity * alloy;


But ... you'd be better off thinking about what happens when you actually go shopping: They maintain a list of everything you buy, not just sum the amount - if your supermarket receipt just said "$406.99" then you'd probably not be happy, particularly if you needed to return something...
 
Share this answer
 
v2
Comments
CPallini 15-Apr-20 3:39am    
:-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900