修改编程挑战3以使用两个并行数组的Product对象数组。 Product类需要成员变量来保存产品名称和数量。
挑战3:编写一个程序,让一个芯片和莎莎制造商跟踪他们生产的五种不同类型的莎莎的销售情况:温和,中等,甜味,热辣和刺激。它应该使用两个并行的五元素数组:一个包含五个莎莎名字的字符串数组和一个整数数组,它包含过去一个月中每个萨尔萨类型销售的罐子数量。在创建名称数组时,应使用初始化列表存储salsa名称。该程序应提示用户输入每种类型的销售数量。已输入此销售数据,该程序应生成一份报告,显示每种莎莎类型的销售额,总销售额以及最畅销和最低销售产品的名称。
我做了挑战3,代码here。
经过多次尝试,我似乎无法让所述程序发挥作用。我已经评论了这些错误,以及它们是什么。这是我到目前为止所得到的:
#ifndef SALSA_H
#define SALSA_H
class Salsa
{
private:
void getTotal();
void getHigh();
void getLow();
int count;
int total;
int high;
int low;
int flavor;
public:
void getSold();
};
#endif
#include "Salsa.h"
#include <iostream>
#include <string>
using namespace std;
void Salsa::getSold()
{
for (count = 0; count < 5; count++)
{
cout << "Jar sold last month of ";
cout << count + 1;
cin >> flavor[count]; //Get error saying subscript array or pointer type
while (flavor[count] <= 0) // Get error saying subscript array or pointer type
cout << "Jars sold must be greater than or equal to 0.";
cout << "Re-enter jars sold for last month ";
cin >> flavor[count];
cout << endl;
}
}
Salsa::getTotal();
Salsa::getHigh();
Salsa::getLow();
}
void Salsa::getTotal()
total = 0;
for (count = 0; count < 5; count++)
total += flavor[count];
cout << "Total Sales: " << total << endl;
}
void Salsa::getHigh()
{
highest = flavor[0];
int index = 1;
for (count = 0; count < 5; count++)
if (flavor[count] > highest)
{
highest = flavor[count];
index = count + 1;
}
cout << "High Seller: " << flavor << endl;
}
void Salsa::getLow()
{
lowest = flavor[0];
int index = 1;
for (count = 0; count < 5; count++)
if (flavor[count] < lowest)
{
lowest = flavor[count];
index = count + 1;
}
cout << "Low Seller: " << flavor << endl;
}
#include "Salsa.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
const int SALS_FLAV = 5;
string flavor[SALS_FLAV] = { "mild", "medium", "sweet", "hot", "zesty" };
Salsa sold;
for (int index = 0; index < SALS_FLAV; index++)
getSold(flavor[SALS_FLAV]); // Get error saying 'getSold' identifier not found
sold.getSold();
return 0;
}
答案 0 :(得分:1)
您的代码存在许多问题。以下代码可以执行您想要的操作。
你应该研究它并尝试改进它。其中一个改进可能是使用std::vector
而不是数组。这意味着您可以避免手动内存管理(新建/删除),从而实现不必定义析构函数的理想。
#include <iostream>
#include <string>
using namespace std;
class Salsa
{
public:
Salsa(string* flavours, int num_flavours);
~Salsa();
void getSold();
private:
void getTotal();
void getHigh();
void getLow();
string* flavours_;
int num_flavours_;
int* sold_count_;
};
Salsa::Salsa(string* flavours, int num_flavours)
{
num_flavours_ = num_flavours;
flavours_ = new string[num_flavours_];
sold_count_ = new int[num_flavours_];
int i;
for(i = 0; i < num_flavours_; i++)
{
flavours_[i] = flavours[i];
}
}
Salsa::~Salsa()
{
delete[] flavours_;
delete[] sold_count_;
}
void Salsa::getSold()
{
int count;
int num;
for (count = 0; count < num_flavours_; count++)
{
cout << "Jar sold last month of " << flavours_[count] << " ";
cin >> num;
while(num <= 0)
{
cout << "Jars sold must be greater than or equal to 0." << endl;
cout << "Re-enter jars sold for last month " << endl;
cin >> num;
}
sold_count_[count] = num;
}
getTotal();
getHigh();
getLow();
}
void Salsa::getTotal()
{
int count;
int total = 0;
for (count = 0; count < num_flavours_; count++)
total += sold_count_[count];
cout << "Total Sales: " << total << endl;
}
void Salsa::getHigh()
{
int count;
int highest = sold_count_[0];
int index = 0;
for (count = 0; count < num_flavours_; count++)
{
if (sold_count_[count] > highest)
{
highest = sold_count_[count];
index = count;
}
}
cout << "High Seller: " << flavours_[index] << endl;
}
void Salsa::getLow()
{
int count;
int lowest = sold_count_[0];
int index = 0;
for (count = 0; count < num_flavours_; count++)
{
if (sold_count_[count] < lowest)
{
lowest = sold_count_[count];
index = count;
}
}
cout << "Low Seller: " << flavours_[index] << endl;
}
int main()
{
const int SALS_FLAV = 5;
string flavor[SALS_FLAV] = { "mild", "medium", "sweet", "hot", "zesty" };
Salsa sold(flavor, SALS_FLAV);
sold.getSold();
return 0;
}