我不知道我的方法输入无限量的项目价格有什么问题。然而,最大的担忧是当我按零(它需要终止的方式)时,输入不会停止,并且不显示最终输出。任何方向更好的方法将不胜感激。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;
}
使用矢量来为您调整自己的东西。 – chris 2013-02-13 01:26:15
该阵列似乎很好,我检查了。但是,我无法得到零输入来终止循环。我希望它尽可能简单。 – Klinetel 2013-02-13 01:27:42
好吧,'double * prices = new double [];'甚至不是有效的语法。 – chris 2013-02-13 01:32:55