2014-03-05 19 views
0

我是新来编程,我试图让这个程序加起来咖啡的价格,以增加额外的东西,如肉桂等,但一旦程序运行它并没有增加额外的咖啡价格,我已经在它上面好几个小时了,我很难过。就像我说的,我是新手,所以如果有人可以帮助并解释为什么它不工作,或者我需要解决这个问题,那将会很棒。谢谢! 代码:我的c + +程序不会加起来我的总价

#include <iostream> 
#include <cstdlib> 
using namespace std; 
int main() 
{ 
    const int SIZE = 5; 
    double COFFEEPRICE = 2.00; 
    string products[SIZE]= {"Whipped cream", "Cinnamon", "Chocolate sauce", "Amaretto", "Irish whiskey"}; 
    double prices[SIZE]={0.89, 0.25, 0.59, 1.50, 1.75}; 
    int totalPrice = 0; 
    int choice = 0; 
    int SENTINEL = -1; 
    { 
     while (choice <= SENTINEL) 
      cout << "Please select an item from the Product menu by selecting the item number (1 - 5) or -1 to terminate: " ; 
     cout << "Product   Price ($)" << endl; 
     cout << "=======   =========" << endl; 
     cout << "1. Whipped cream  0.89" << endl; 
     cout << "2. Cinnamon   0.25" << endl; 
     cout << "3. Chocolate sauce 0.89" << endl; 
     cout << "4. Amaretto   1.50" << endl; 
     cout << "5. Irish whiskey  1.75" << endl; 
     cout << "Please enter a positive number: " << endl; 
     cin >> choice; 

     if (choice > SENTINEL) 
     { 
      if ((choice >= 1) && (choice <= 5)) 
      { 
       totalPrice = totalPrice + prices[choice-1]; 
       cout << "Item number " << choice << ": "<< products[choice-1] << " has been added" << endl; 
      } 
      else 
      { 
       cout << "Item number ",choice, " is not valid", "Sorry we do not carry that item" ; 
      } 
     } 

     totalPrice + COFFEEPRICE; 
     cout << "Total price of order is $" << totalPrice << endl; 
     cout << "Thanks for purchasing from Jumpin Jive Coffee Shop" << endl; 

    } 
    system("pause"); 
    return 0; 
} 

回答

0

您需要更改

totalPrice + COFFEEPRICE;    // Problem: totalPrice will not change!!! 

totalPrice = totalPrice + COFFEEPRICE; // add and update by assign back 

totalPrice += COFFEEPRICE;   // equivalent to the above 

为了与新加入v要更新totalPrice ALUE。

0

您必须分配新的价格,这是

totalPrice + COFFEEPRICE 

旧值。所以写:

totalPrice = totalPrice + COFFEEPRICE; // note, 
             // same as totalPrice += COFFEEPRICE; 

否则,表达式totalPrice + COFFEEPRICE;没有任何用处。

而且,我不知道这个对你的工作(这不会进入while():?可能只是用于调试):

int choice = 0; 
int SENTINEL = -1; 
    { 
     while (choice <= SENTINEL) 
0

发现了太多的错误,以便而不是指向每一个...我已作出更改并发布正确的代码...

首先和主要的一个将循环... 你没有得到任何来自用户的输入并检查他是否要退出(通过输入负数).. .way你用while while循环是不正确的...

do while这里更有意义....也总totalPrice + COFFEEPRICE;你需要在一些地方保存这个值,这样就可以再次使用它 double totalPrice; totalPrice = totalPrice + COFFEEPRICE;

#include <iostream> 
#include <cstdlib> 
using namespace std; 
int main() 
{ 

    const int SIZE = 5; 
    double COFFEEPRICE = 2.00; 
    string products[SIZE]= {"Whipped cream", "Cinnamon", "Chocolate sauce", "Amaretto", "Irish whiskey"}; 
    double prices[SIZE]={0.89, 0.25, 0.59, 1.50, 1.75}; 
    double totalPrice = 0; 
    int choice = 0; 
    int SENTINEL = -1; 

    do 
    { 
     cout << "Please select an item from the Product menu by selecting the item number (1 - 5) or -1 to terminate: " ; 
     cout << "Product   Price ($)" << endl; 
     cout << "=======   =========" << endl; 
     cout << "1. Whipped cream  0.89" << endl; 
     cout << "2. Cinnamon   0.25" << endl; 
     cout << "3. Chocolate sauce 0.89" << endl; 
     cout << "4. Amaretto   1.50" << endl; 
     cout << "5. Irish whiskey  1.75" << endl; 
     cout << "Please enter a positive number: " << endl; 
     cin >> choice; 


     if (choice > SENTINEL) 
     { 
      if ((choice >= 1) && (choice <= 5)) 
      { 
       totalPrice = totalPrice + prices[choice-1]; 
       cout << "Item number " << choice << ": "<< products[choice-1] << " has been added" << endl; 
      } 
      else 
      { 
       cout << "Item number ",choice, " is not valid", "Sorry we do not carry that item" ; 
      } 
     } 

    totalPrice = totalPrice + COFFEEPRICE; 
    cout << "Total price of order is $" << totalPrice << endl; 
    cout << "Thanks for purchasing from Jumpin Jive Coffee Shop" << endl; 

    }while (choice <= SENTINEL); 
//system("pause"); 
return 0; 
} 
相关问题