C ++数组输入和退出验证

时间:2013-02-13 01:23:52

标签: c++ arrays visual-studio visual-c++ validation

我不确定我输入无限量商品价格的方法有什么问题。然而,最大的问题是当我按下零(它需要终止的方式)时,输入不会停止,并且不显示最终输出。任何更好的方法都会受到赞赏。

#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 个答案:

答案 0 :(得分:1)

;应为if(prices[i] == 0){ check = false; break; }

答案 1 :(得分:1)

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

    check=false;


    break;

}

应该解决这个问题,并且它不会在价格数组中加零。