首页 > 解决方案 > 重复下单后总价会累积但产生错误值

问题描述

我的代码有逻辑错误(有时在重复订单时)。它计算第一个订单的价格罚款。但是当我重复订单时(在函数 listHardware 中使用 while 循环),总价格将被弄乱,并且不会产生正确的值(价格)。我在 calcFunc 中的计算或返回值有问题吗?

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;
    const double discount=0.1;

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

    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;
    }

    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 for now (not including membership discount): RM" << price << endl;
    cout << setfill ('-') << setw (55) << "-" << endl;

    cout << "Do you want to purchase more? Press y for yes." << endl;
    cin >> addorder;
    }
    cout << setfill ('-') << setw (55) << "-" << endl;
    if (shopmember == 'y' || shopmember == 'Y')
        cout << "Total Price: RM" << price-(price*discount) << endl;
    else
        cout << "Total Price: RM" << 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)
{
    if(m_type==1) //monitor
    {
        price+=3000*quantity;
    }
    else if(m_type==2) //monitor
    {
        price+=4000*quantity;
    }
    else if(m_type==3) //monitor
    {
        price+=8000*quantity;
    }

    if(c_type==1) //cpu
    {
        price+=1200*quantity;
    }
    else if(c_type==2) //cpu
    {
        price+=1200*quantity;
    }
    else if(c_type==3) //cpu
    {
        price+=8000*quantity;
    }

    if(r_type==1) //ram
    {
        price+=400*quantity;
    }
    else if(r_type==2) //ram
    {
        price+=1200*quantity;
    }
    else if(r_type==3) //ram
    {
        price+=2000*quantity;
    }

    if(s_type==1) //ssd
    {
        price+=1250*quantity;
    }
    else if(s_type==2) //ssd
    {
        price+=600*quantity;
    }
    else if(s_type==3) //ssd
    {
        price+=1600*quantity;
    }

    if(g_type==1) //gpu
    {
        price+=4000*quantity;
    }
    else if(g_type==2) //gpu
    {
        price+=2900*quantity;
    }
    else if(g_type==3) //gpu
    {
        price+=2100*quantity;
    } 
    return price;
}

标签: c++function

解决方案


首先,您不会初始化*_type变量,因此它们具有不可预测的值,这可能会影响从calcFunc().

*_type其次,进入循环时您不会重置变量,因此如果在第一次迭代中您选择了监视器类型 2,即,那么在第二次迭代中,即使在第二次迭代中您选择了 cpu,m_type = 2它也会再次传递给它。calcFunc()显示器将包含在总价中两次。


推荐阅读