2013-02-13 84 views
0

我不知道我的方法輸入無限量的項目價格有什麼問題。然而,最大的擔憂是當我按零(它需要終止的方式)時,輸入不會停止,並且不顯示最終輸出。任何方向更好的方法將不勝感激。C++數組輸入和退出驗證

#include <iostream> 
#include <iomanip> 

using namespace std; 

int main() 
{ 
const int MAX=8; 
bool check=true; 
double totalPrice, discount, discountedPrice; 
double *prices = new double[]; 

cout<<"Enter the price of your items."; 
cout<<"Enter zero (0) when you have finished entering all the items.\n\n"; 

while(check==true) 
{ 

for(int i=0;i<MAX;i++) 
{ 

    cout<<"Enter the price of item "<<i<<": "; 
    cin>>prices[i]; 

if(cin==0){ 

    check=false; 


    break; 

} 
} 

    if(prices==0) 
    { 
     check=false; 
     break; 

     for(int j=0; j<sizeof(prices);j++) 
     { 
      totalPrice+=prices[j]; 
     } 

     discount=totalPrice*.075; 
     discountedPrice=totalPrice-discount; 

     cout<<"The total price of your "<<sizeof(prices)<<"items is: $"<<totalPrice; 
     cout<<"Discount of your "<<sizeof(prices)<<"is: $"<<discount; 
     cout<<"\nThe discounted price of your "<<sizeof(prices)<<"is: $"<<discountedPrice; 
    } 
} 

cin.get(); 
cin.get(); 

return 0; 
} 
+2

使用矢量來爲您調整自己的東西。 – chris 2013-02-13 01:26:15

+0

該陣列似乎很好,我檢查了。但是,我無法得到零輸入來終止循環。我希望它儘可能簡單。 – Klinetel 2013-02-13 01:27:42

+0

好吧,'double * prices = new double [];'甚至不是有效的語法。 – chris 2013-02-13 01:32:55

回答

1

;它應該是​​

+0

謝謝你,在弄亂它之後,你的邏輯運作良好。 – Klinetel 2013-02-13 01:43:25

1
cout<<"Enter the price of item "<<i<<": "; 
    int temp; 
    cin>>temp; 
if(temp == 0){ 

    check=false; 


    break; 

} 

應該可以解決這個問題,它不會在價格數組中放一個零。