如果用户重复订购,价格不会累积

时间:2018-12-06 10:27:51

标签: c++

首先,抱歉,这是一个愚蠢的问题。

这是我的2个功能。我正在尝试在功能列表硬件中重复订购,如您所见,“您想购买更多产品吗?按y表示是”。在功能的底部。但是,如果用户重复订购,则只会显示新价格。为什么它不累积其先前订单的先前价格?

我认为我通过在calcFunc中执行price + =来做到这一点。

void listHardware()
{
    int i,j,type,m_type,c_type,r_type,s_type,g_type,quantity=0;
    const int SIZE=3;
    char shopmember,addorder = 'y';
    double price=0;

    while(addorder=='Y' || addorder=='y')
    {
    cout << endl << "Select which type of hardware that you want to purchase: ";
    cin >> type;

    if(type==1)
    {
        const char *monitor[SIZE][2]=
        {
            {"BenQ PD3200U", "(RM3000)"},
            {"Acer Predator X34", "(RM4000)"},
            {"Dell UltraSharp UP3218K", "(RM8000)"}
        };

        cout << "Monitors:" << endl;
        for(i=0;i<SIZE;i++)
        {
            cout << "\t" << i+1 << ". ";
            for(j=0;j<2;j++)
            {
                cout << monitor[i][j] << " ";
            }
            cout << endl;
        }
        cout << endl << "Enter which Monitor you would like to purchase: ";
        cin >> m_type; //monitor
        cout << endl << "How many Monitor would you like to purchase?" << endl;
        cin >> quantity;
    }
    if(type==2)
    {
        const char *cpu[SIZE][2]=
        {
            {"AMD Ryzen 7 2700X", "(RM1200)"},
            {"Intel Core i5-8600K", "(RM1200)"},
            {"Intel Core i9-7980XE", "(RM8000)"}
        };

        cout << "CPU:" << endl;
        for(i=0;i<SIZE;i++)
        {
            cout << "\t" << i+1 << ". ";
            for(j=0;j<2;j++)
            {
                cout << cpu[i][j] << " ";
            }
            cout << endl;
        }
        cout << endl << "Enter which CPU would you like to purchase: ";
        cin >> c_type; //cpu
        cout << endl << "How many CPU would you like to purchase?" << endl;
        cin >> quantity;
    }
    if(type==3)
    {
        const char *ram[SIZE][2]=
        {
            {"Patriot Viper Elite 8GB DDR4-2400MHz", "(RM400)"},
            {"G.Skill Ripjaws V 16GB DDR4-2400MHz", "(RM1200)"},
            {"Corsair Dominator Platinum 32GB DDR4-3333MHz", "(RM2000)"}
        };

        cout << "RAM:" << endl;
        for(i=0;i<SIZE;i++)
        {
            cout << "\t" << i+1 << ". ";
            for(j=0;j<2;j++)
            {
                cout << ram[i][j] << " ";
            }
            cout << endl;
        }
        cout << endl << "Enter which RAM would you like to purchase: ";
        cin >> r_type; //ram
        cout << endl << "How many RAM would you like to purchase?" << endl;
        cin >> quantity;
    }
    if(type==4)
    {
        const char *ssd[SIZE][2]=
        {
            {"Samsung 860 Pro 1TB", "(RM1250)"},
            {"Crucial MX500 1TB", "(RM600)"},
            {"WD Blue 2TB", "(RM1600)"}
        };

        cout << "SSD:" << endl;
        for(i=0;i<SIZE;i++)
        {
            cout << "\t" << i+1 << ". ";
            for(j=0;j<2;j++)
            {
                cout << ssd[i][j] << " ";
            }
            cout << endl;
        }
        cout << endl << "Enter which SSD would you like to purchase: ";
        cin >> s_type; //ssd
        cout << endl << "How many SSD would you like to purchase?" << endl;
        cin >> quantity;
    }
    if(type==5)
    {
        const char *gcard[SIZE][2]=
        {
            {"Nvidia GeForce RTX 2080 Ti", "(RM4000)"},
            {"Nvidia GeForce GTX 1080 Ti", "(RM2900)"},
            {"AMD Radeon RX 580 8GB", "(RM2100)"}
        };

        cout << "Graphic Card:" << endl;
        for(i=0;i<SIZE;i++)
        {
            cout << "\t" << i+1 << ". ";
            for(j=0;j<2;j++)
            {
                cout << gcard[i][j] << " ";
            }
            cout << endl;
        }
        cout << endl << "Enter which Graphic Card would you like to purchase: ";
        cin >> g_type; //gpu
        cout << endl << "How many Graphic Card would you like to purchase?" << endl;
        cin >> quantity;
    }

    cout << "Membership (Y/N): ";
    cin >> shopmember;

    cout << "Do you want to purchase more? Press y for yes." << endl;
    cin >> addorder;
    cin.ignore();

    price=calcFunc(m_type,c_type,r_type,s_type,g_type,quantity,price,shopmember); //function call
    }

    cout << setfill ('-') << setw (55) << "-" << endl;
    cout << "Total Price: " << price << endl;
    cout << setfill ('-') << setw (55) << "-" << endl;
}

//

double calcFunc(int m_type,int c_type,int r_type, int s_type, int g_type,int quantity,double price, char shopmember)
{
    const double discount=0.1;

    if(m_type==1) //monitor
    {
        if (shopmember == 'y' || shopmember == 'Y')
            price+=(3000-(3000*discount))*quantity;
        else
            price+=3000*quantity;
    }
    else if(m_type==2) //monitor
    {
        if (shopmember == 'y' || shopmember == 'Y')
            price+=(4000-(4000*discount))*quantity;
        else
            price+=4000*quantity;
    }
    else if(m_type==3) //monitor
    {
        if (shopmember == 'y' || shopmember == 'Y')
            price+=(8000-(8000*discount))*quantity;
        else
            price+=8000*quantity;
    }

    if(c_type==1) //cpu
    {
        if (shopmember == 'y' || shopmember == 'Y')
            price+=(1200-(1200*discount))*quantity;
        else
            price+=1200*quantity;
    }
    else if(c_type==2) //cpu
    {
        if (shopmember == 'y' || shopmember == 'Y')
            price+=(1200-(1200*discount))*quantity;
        else
            price+=1200*quantity;
    }
    else if(c_type==3) //cpu
    {
        if (shopmember == 'y' || shopmember == 'Y')
            price+=(8000-(8000*discount))*quantity;
        else
            price+=8000*quantity;
    }

    if(r_type==1) //ram
    {
        if (shopmember == 'y' || shopmember == 'Y')
            price+=(400-(400*discount))*quantity;
        else
            price+=400*quantity;
    }
    else if(r_type==2) //ram
    {
        if (shopmember == 'y' || shopmember == 'Y')
            price+=(1200-(1200*discount))*quantity;
        else
            price+=1200*quantity;
    }
    else if(r_type==3) //ram
    {
        if (shopmember == 'y' || shopmember == 'Y')
            price+=(2000-(2000*discount))*quantity;
        else
            price+=2000*quantity;
    }

    if(s_type==1) //ssd
    {
        if (shopmember == 'y' || shopmember == 'Y')
            price+=(1250-(1250*discount))*quantity;
        else
            price+=1250*quantity;
    }
    else if(s_type==2) //ssd
    {
        if (shopmember == 'y' || shopmember == 'Y')
            price+=(600-(600*discount))*quantity;
        else
            price+=600*quantity;
    }
    else if(s_type==3) //ssd
    {
        if (shopmember == 'y' || shopmember == 'Y')
            price+=(1600-(1600*discount))*quantity;
        else
            price+=1600*quantity;
    }

    if(g_type==1) //gpu
    {
        if (shopmember == 'y' || shopmember == 'Y')
            price+=(4000-(4000*discount))*quantity;
        else
            price+=4000*quantity;
    }
    else if(g_type==2) //gpu
    {
        if (shopmember == 'y' || shopmember == 'Y')
            price+=(2900-(2900*discount))*quantity;
        else
            price+=2900*quantity;
    }
    else if(g_type==3) //gpu
    {
        if (shopmember == 'y' || shopmember == 'Y')
            price+=(2100-(2100*discount))*quantity;
        else
            price+=2100*quantity;
    }
}

1 个答案:

答案 0 :(得分:1)

double calcFunc(int m_type,int c_type,int r_type, int s_type, int g_type,int quantity,double price, char shopmember)

缺少返回语句。 添加

return price;

在函数末尾,您应该会很好。