c++/switch语句例题

/问题是:
一家公司销售5种不同的产品零售价分别是:产品1,2.98美元;产品2,4.50美元;产品3,9.98美元
;产品4,4.49美元;产品5,6.87美元。请编写一个程序,要求用户输入一系列如下所示的数据:
a)产品编号
b)销售量
您的程序应计算并显示所有销售产品的总零售价值。如果销售数量为负数,则将其设置为0。当程序读取产品数等于-l时,停止循环并显示最终结果。
这是我写的程序:
/

#include
using namespace std;



int main() {
    double sum1=0, sum2=0, sum3=0 , sum4=0, sum5=0 ;
    int productnum;
    cin >>productnum;
    int i = 0;
    
        for (i=0;i<7;i++)
        {
            switch (productnum) {
                int qua;
            case 1:
                cin >> qua;
                {
                    double sum1 = qua * 2.98; break; }

            case 2:
                cin >> qua;
                {
                    double sum2 = qua * 4.50; break;  }
            case 3:
                cin >> qua;
                {
                    double sum3 = qua * 9.98;  break; }
            case 4:
                cin >> qua;
                {
                    double sum4 = qua * 4.49;  break; }
            case 5:
                cin >> qua;
                {
                    double sum5 = qua * 6.87; break; }


            }
        }
    
    
    double sum;
    sum = sum1 + sum2 + sum3 + sum4 + sum5;
    cout << sum << endl;
    system("pause");
    return 0;
}

后面的两个要求都还没有写。想请教大家我这个程序有什么问题,为什么无法计算五种产品销售额的总量?


int main() {
    double sum1=0, sum2=0, sum3=0 , sum4=0, sum5=0 ;
    int productnum;

    int i = 0;

    for (i; i < 5; i++)
    {

        cin >> productnum;
        switch (productnum)
        {
            double qua;
        case 1:
            cin >> qua;
            {
                sum1 = qua * 2.98;
                break;
            }

        case 2:
            cin >> qua;
            {
                sum2 = qua * 4.50;
                break;
            }
        case 3:
            cin >> qua;
            {
                sum3 = qua * 9.98;
                break;
            }
        case 4:
            cin >> qua;
            {
                sum4 = qua * 4.49;
                break;
            }
        case 5:
            cin >> qua;
            {
                sum5 = qua * 6.87;
                break;
            }
        }
        }
    
    
    double sum;
    sum = sum1 + sum2 + sum3 + sum4 + sum5;
    cout << sum << endl;
    system("pause");
    return 0;
}

img

望采纳!

for (i=0;i<7;i++) 这个是干嘛?
将每个case内部的,如 double sum1 = 改成 sum1 += 因为你设置成了临时局部变量

以下仅供参考:

#include<iostream>
using namespace std;

class HovertreeProduct
{
public:
    HovertreeProduct() {
        h_productId = 0;
        h_price = 0;
        h_name = "产品0";
    }
    HovertreeProduct(int htId) {
        h_productId = htId;
        h_price = 0;
        h_name = "产品" + htId;
    }


    void SetPrice(double htPrice) {
        if (htPrice < 0)
        {
            h_price = 0;
        }
        h_price = htPrice;
    }
    double GetPirce()
    {
        return h_price;
    }

private :
    int h_productId;
    double h_price;
    string h_name;

};

class HovertreeProductInfo 
{
public :
    HovertreeProductInfo(HovertreeProduct htProduct){
        h_htProduct = htProduct;
        h_totalCount = 0;
    }

    void HtAddCount(int htCount) {
        h_totalCount = h_totalCount + htCount;
        if (h_totalCount < 0)
            h_totalCount = 0;//如果销售数量为负数,则将其设置为0。 by 何问起
    }

    double GetTotalPrice()
    {
        return  h_htProduct.GetPirce()* h_totalCount;
    }

    /*string GetProcuctInfo()
    {
        string m_info = "";
    }*/

private:
    int h_totalCount;
    HovertreeProduct h_htProduct;
    
};

int main() {

    HovertreeProduct m_htProduct1(1);
    m_htProduct1.SetPrice(2.98);
    HovertreeProduct m_htProduct2(2);
    m_htProduct2.SetPrice(4.50);
    HovertreeProduct m_htProduct3(3);
    m_htProduct3.SetPrice(9.98);
    HovertreeProduct m_htProduct4(4);
    m_htProduct4.SetPrice(4.49);
    HovertreeProduct m_htProduct5(5);
    m_htProduct5.SetPrice(6.87);

    HovertreeProductInfo m_htProductInfo1(m_htProduct1);
    HovertreeProductInfo m_htProductInfo2(m_htProduct2);
    HovertreeProductInfo m_htProductInfo3(m_htProduct3);
    HovertreeProductInfo m_htProductInfo4(m_htProduct4);
    HovertreeProductInfo m_htProductInfo5(m_htProduct5);

    int m_productId, m_count;
    cout << "请输入产品编号(1,2,3,4,5)" << endl;
    cin >> m_productId;
    cout << "请输入产品销售量(整数,输入-1完成)" << endl;
    cin >> m_count;
    while (m_count != -1)//程序读取产品数等于-l时,停止循环并显示最终结果。 by 何问起
    {
        switch (m_productId)
        {
        case 1:
            m_htProductInfo1.HtAddCount(m_count);
            break;
        case 2:
            m_htProductInfo2.HtAddCount(m_count);
            break;
        case 3:
            m_htProductInfo3.HtAddCount(m_count);
            break;
        case 4:
            m_htProductInfo4.HtAddCount(m_count);
            break;
        case 5:
            m_htProductInfo5.HtAddCount(m_count);
            break;
        default:
            cout << "没有该编号的产品" << endl;
            break;
        }
        cout << "请输入产品编号(1,2,3,4,5)" << endl;
        cin >> m_productId;
        cout << "请输入产品销售量(整数,输入-1完成)" << endl;
        cin >> m_count;
    }
    double m_totalPrice = m_htProductInfo1.GetTotalPrice()
        + m_htProductInfo2.GetTotalPrice()
        + m_htProductInfo3.GetTotalPrice()
        + m_htProductInfo4.GetTotalPrice()
        + m_htProductInfo5.GetTotalPrice();
    cout << "总零售价值:" << m_totalPrice << endl;
    cout << "何问起提示:输入任何内容,回车退出。 "<<endl;
    string m_temp;
    cin >> m_temp;
    
    return 0;
}

实际效果:

img