[Solved] Chips and Salsa using header file


There are many issues with your code. The following code may do what you want.

You should study it and try to improve it. One such improvement could be the use of std::vector as opposed to arrays. This will mean you can avoid manual memory management (new/delete) and therefore achieve the ideal of not having to define a destructor.

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

1

solved Chips and Salsa using header file