我没有得到产量最低的产品.我该如何纠正它?最后一行是最低销售产品的输出.谢谢


  1. 程序应该提示用户输入每种类型销售的罐子数量。该程序应生成一份报告,显示每种莎莎酱类型的销售额、总销售额以及销量最高和最低销量的产品的名称。
#include <iostream>
#include<string>
using namespace std;
int main()
{
const int SIZE=5;
string salsa_names[SIZE] = { "mild","medium","sweet","hot","zesty" },name1,name2;
double number_of_jars_sold[SIZE],total=0;   
cout << "enter the number of jars sold for each of different types of salsan";
for (int i = 0; i < SIZE; i++)
{
cout <<"The salsa names are :"<<"'"<<salsa_names[i]<<"'";
cin >> number_of_jars_sold[i];
total += number_of_jars_sold[i];
}
double large=number_of_jars_sold[0],small=number_of_jars_sold[0];
cout << "The sales for each of salsa type is ="<<endl;
for (int i=0;i<SIZE;i++)
{
cout << salsa_names[i] << " : " << number_of_jars_sold[i] << endl;
}
cout << "total sale is equal to" << total << endl;
for (int i = 0; i < SIZE; i++)
{
if (large < number_of_jars_sold[i])
{
large = number_of_jars_sold[i];
name1=salsa_names[i];
}
}
for (int j = 0; j < SIZE; j++)
{
if (small > number_of_jars_sold[j])
{
small = number_of_jars_sold[j];
name2 = salsa_names[j];
}
}
cout << "The name of highest selling product is : " << name1 << endl;
cout << "The name of lowest selling product is : " << name2 << endl;
return 0;
}

已编辑:: 您应该首先将小和大初始化为,在此行之前添加一个 if 条件if (small > number_of_jars_sold[j])if(small == 0),因此您将在小变量中具有最小值。

最新更新