重复下单后的总价会累积但产生错误的值

时间:2018-12-06 15:28:01

标签: c++ function

我的代码有逻辑错误(有时在重复订购时)。它计算第一笔订单的罚款价格。但是,当我重复执行订单(在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;
}

1 个答案:

答案 0 :(得分:0)

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

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