Chips和Salsa使用头文件

Chips and Salsa using header file

本文关键字:文件 Salsa Chips      更新时间:2023-10-16

修改编程挑战3,使用两个并行数组组成的Product对象数组instad。Product类将需要成员变量来保存产品名称和数量。

挑战3:编写一个程序,让薯条和莎莎酱制造商跟踪他们生产的五种不同类型莎莎酱的销售情况:温和、中等、甜、辣和辣。它应该使用两个平行的五元素数组:一个字符串数组包含五个salsa名称,另一个整数数组包含上个月每个salsa类型售出的罐子数量。salsa名称应在创建名称数组时使用初始化列表进行存储。该程序应提示用户输入每种类型的罐子销售数量。一旦输入了这些销售数据,程序就会生成一份报告,显示每种莎莎酱的销售额、总销售额以及最高销售额和最低销售额产品的名称。

我完成了挑战3,代码在这里。

经过多次尝试,我似乎无法使上述程序发挥作用。我已经评论了这些错误,以及它们是什么。到目前为止,我得到的是:

#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;
}

您的代码有很多问题。以下代码可以执行您想要的操作。

你应该研究并尝试改进它。其中一个改进可能是使用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;
}