C++ C++;数组输入和退出验证
我不确定我输入无限量商品价格的方法有什么问题。然而,最大的问题是当我按零(它需要终止的方式)时,输入不会停止,最终输出也不会显示。如能提供更好的方法,我们将不胜感激C++ C++;数组输入和退出验证,c++,arrays,visual-studio,visual-c++,validation,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;
#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;
}
#包括
#包括
使用名称空间std;
int main()
{
常数int MAX=8;
布尔检查=真;
双倍总价、折扣、折扣价格;
双倍*价格=新的双倍[];
cout;应该是如果(prices[i]==0){check=false;break;}
可以为您调整大小的对象使用向量。数组似乎很好,我检查过了。但是,我无法获得零的输入来终止循环。我希望它尽可能简单。嗯,double*prices=new double[];
甚至都不是有效的语法。你有什么建议吗?这个数组需要处理十进制值,到目前为止它已经工作了。我已经建议了std::vector
。我不知道这是否是MSVC扩展,但是vector可以通过push_back
或任何你喜欢的插入方法来实现。谢谢你,在搞乱之后它,你的逻辑运作良好。
cout<<"Enter the price of item "<<i<<": ";
int temp;
cin>>temp;
if(temp == 0){
check=false;
break;
}