首页 > 解决方案 > 如何让价格在 C++ 中一遍又一遍地增加?

问题描述

我只是 C++ 的初学者。如果我确认购买,我仍然无法添加价格。如果我决定确认购买,我想保留价格。如果我想买更多的东西,然后再买同样的东西,比如我的选择是1,买了1,确认购买,想买更多的东西,决定再买同样的东西就是1 ,我想将第一次购买添加到我的最后一次购买中。如果我想一遍又一遍地购买相同的东西,没有限制,除非我不想查看更多物品。

  #include <iostream>

    using namespace std;

    //My Function
    double Peripheral (int quantity, double price) {
        double result;
        result = quantity * price;
        return result;
    }

    int main()
    {
        int choice;
        int purchase;
        int quantity;
        double totalPeripheral1 = 0;
        double totalPeripheral2 = 0;
        double totalPeripheral3 = 0;
        double totalPeripheral4 = 0;
        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 >> quantityPeripheral1;
            totalPeripheral1 += Peripheral(quantityPeripheral1, alloy);
            cout << "The total price for that is " << totalPeripheral1 << endl;
            cout << "Confirm the Purchase? [Y]/[N]: ";
            cin >> confirm;

            if (confirm == 'Y') {
                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;
            }
        }
         cout << totalPeripheral1 << endl;
}
}

     }

    }

        while (purchase == 5 || buyMore == 'Y' || look == 'Y');
          cout << "--------------------" << endl;

标签: c++

解决方案


如果要添加,则必须添加

totalPeripheral1 = Peripheral(quantityPeripheral1, alloy);

应该

totalPeripheral1 += Peripheral(quantityPeripheral1, alloy);

=将右侧的值复制到左侧的变量中,这称为赋值。但是+=将右侧的值添加到左侧的变量中。

显然你不需要四个外围变量,只需要一个。


推荐阅读