使用哨兵控制的环/开关时出错

时间:2014-02-24 23:37:18

标签: c++

邮购商店出售五种不同的零售价格产品 是:产品1 - $ 2.98,产品2- $ 4.50,产品3- $ 9.98,产品4- $ 4.49和产品5- $ 6.87。编写一个程序,读取一系列数字,如下所示: a)产品编号 b)销售数量 您的程序应使用switch语句来确定每个产品的零售价格。您的 程序应计算并显示所有销售产品的总零售价值。使用哨兵控制的循环来确定程序何时应该停止循环并显示最终结果。

当我尝试编译代码时出现了大量错误,我想知道我做错了什么。

#include <iostream>
using namespace std;

int main()
{
int numberOfProducts = 0;
int costOfProducts = 0;
int productTotal = 0;
double amountP1 = 0;
double amountP2 = 0;
double amountP3 = 0;
double amountP4 = 0;
double amountP5 = 0;
double product1 = 2.98;
double product2 = 4.50;
double product3 = 9.98;
double product4 = 4.49;
double product5 = 6.87;
cout<<"How many products do you want to buy? -1 to finish shopping"<<endl;
cin>>numberOfProducts;
while(numberOfProducts != -1)
     cout<<"Which products do you want to buy? -1 to finish shopping"<<endl;
     cin>>costOfProducts;
        switch(costOfProducts)
     {
        case product1:
            cout<<"Product 1($2.98) has been purchased";
                productTotal = productTotal + 2.98;
                amountP1 = amountP1 + 1;
                break;
        case product2:
            cout<<"Product 2($4.50) has been purchased";
                productTotal = productTotal + 4.50;
                amountP2 = amountP2 + 1;
                break;
        case product3:
            cout<<"Product 3($9.98) has been purchased";
                productTotal = productTotal + 9.98;
                amountP3 = amountP3 + 1;
                break;
        case product4:
            cout<<"Product 4($4.49) has been purchased";
                productTotal = productTotal + 4.49;
                amountP4 = amountP4 + 1;
                break;
        case product5:
            cout<<"Product 5($6.87) has been purchased";
            productTotal = productTotal + 6.87;
            amountP5 = amountP5 + 1;
                break;
        default:
            cout<<"Sorry, please select a product"; << endl;

     }

     cout<<"The total amount of products bought are: " << numberOfProducts;
     cout<<"The total amount of product 1's bought is: $" << amountP1 << endl;
     cout<<"The total amount of product 2's bought is: $" << amountP2 << endl;
     cout<<"The total amount of product 3's bought is: $" << amountP3 << endl;
     cout<<"The total amount of product 4's bought is: $" << amountP4 << endl;
     cout<<"The total amount of product 5's bought is: $" << amountP5 << endl;
     cout<<"The total price of all your products are: $" << productTotal << endl;
     return 0;

}

2 个答案:

答案 0 :(得分:1)

删除以下行末尾的分号:

  • while(numberOfProducts != -1);
  • switch(costOfProducts);

此外,编译器不理解以下符号(我也不理解):

  • product1
  • product2
  • product3
  • product4
  • product5

顺便说一句,我强烈建议您在那里添加几个cin>>,如果您确实希望这样做...

答案 1 :(得分:1)

您需要了解语句块。

while(numberOfProducts != -1)
     cout<<"Which products do you want to buy? -1 to finish shopping"<<endl;
     cin>>costOfProducts;

我会和你一起玩游戏。您需要插入{来开始一个语句块,并}来结束一个 你的角色是找出他们去的地方。

提示1:while语句中的表达式适用于语句块。

提示2:可以在语句块中放置一个或多个语句。

如果你想作弊,你可以查看while语句的语法,以获取循环中的多个语句。