The program calls for:
Here is what the program calls for:

Write a program that lets a maker of chips and salsa keep track of their sales for 5 different types of salsa they produce: mild, medium, sweet, hot, and zesty. It should use two parallel 5-element arrays: an array of strings that holds the 5 salsa names and an array of integers that holds the number of jars sold during the past month for each salsa type. The salsa names should be stored using an initialization list at the time the name array is created. The program should prompt the user to enter the number of jars sold for each type. Once the sales data has been entered, the program should produce a report that displays sales for each salsa type, total sales, and the names of the highest selling and lowest selling products. Input Validation: Do not accept negative values for number of jars sold.

I get "error C2109: subscript requires array or pointer type" when I try to run the following code. Is it possible to run the array as a function to name of the jars and how much each sold?

#include <iostream>
#include <string>
using namespace std;

void showSold(int sales[], int salsa);

int main()
{
	const int SALSA = 5;
	string name[SALSA] = {"mild", "medium", "sweet", "hot", "zesty"}; //Salsa Types
	int sales[SALSA];
	int sold(),
		total(),
		highest(),
		lowest();

	cout << "Please enter the number of jars sold for each salsa\n";
	for(int count = 0; count < SALSA; count++)
	{
		cout << name[count] << ": ";
		cin >> sales[count];

		while(sales[count] < 0)
		{
			cout << "You must enter a number 0 or greater. Please try again\n";
			cin >> sales[count];
		}
	}

	
	showSold(sales, SALSA);

	return 0;
}

void showSold(int sales, int salsa)
{
	for(int count = 0; count < salsa; count++)
	{
		cout << "The amount each salsa sold are:\n";
		cout << sales[count] << " ";
		cout << endl;
	}
}

Dani AI

Generated

Quick diagnosis and concise fixes for the errors in this thread.

As correctly noticed, the C2109 "subscript requires array or pointer type" comes from treating a scalar like an array inside the function. The function declaration and its definition must agree — if the parameter is meant to be an array, the definition must accept an array or pointer (for example int *sales or int sales[]). Also note the line int sold(), total(), highest(), lowest(); does not declare integer variables — it declares function prototypes. The corrected variable declaration is:

int sold, total, highest, lowest;

A more robust, modern approach is to use containers that carry their size and play nicely with library algorithms. The example below demonstrates producing the report (total, highest, lowest) from already-populated vectors; it avoids array-decay pitfalls and is clearer to read and maintain.

#include <iostream>
#include <vector>
#include <string>
#include <numeric>
#include <algorithm>

void reportSales(const std::vector<std::string>& names,
                 const std::vector<int>& sales)
{
    int total = std::accumulate(sales.begin(), sales.end(), 0);
    auto maxIt = std::max_element(sales.begin(), sales.end());
    auto minIt = std::min_element(sales.begin(), sales.end());
    size_t maxIdx = std::distance(sales.begin(), maxIt);
    size_t minIdx = std::distance(sales.begin(), minIt);

    std::cout << "Sales report:\n";
    for (size_t i = 0; i < sales.size(); ++i)
        std::cout << names[i] << ": " << sales[i] << '\n';
    std::cout << "Total jars: " << total << '\n'
              << "Top seller: " << names[maxIdx] << '\n'
              << "Lowest seller: " << names[minIdx] << '\n';
}

Quick checklist and tips:

  • Ensure function prototypes and definitions match exactly (array vs. pointer).
  • Print headers once (outside loops), not on every iteration.
  • Keep input validation (no negatives) and use size_t for indexes when iterating container sizes.
  • For small beginner code, prefer std::vector or std::array over raw arrays; they avoid common decay/size issues.
  • Compile with warnings enabled to catch the int name() vs int name mistake early.

provided a working fix for the immediate compile error; the suggestions above help make the program safer, clearer, and easier to extend (total, highest, lowest reporting).

Recommended Answers

All 2 Replies

Sales is passed to the function as an integer, but in the line cout << sales[count]; you are treating it like an array.

Add a [] to sales on line 36-this will ensure the function will treat sales as an array.

Try this:

#include <iostream>
#include <string>
using namespace std;

void showSold(int sales[], int salsa);

int main()
{
	const int SALSA = 5;
	string name[SALSA] = {"mild", "medium", "sweet", "hot", "zesty"}; //Salsa Types
	int sales[SALSA];
	int sold(),
		total(),
		highest(),
		lowest();

	cout << "Please enter the number of jars sold for each salsa\n";
	for(int count = 0; count < SALSA; count++)
	{
		cout << name[count] << ": ";
		cin >> sales[count];

		while(sales[count] < 0)
		{
			cout << "You must enter a number 0 or greater. Please try again\n";
			cin >> sales[count];
		}
	}

	
	showSold(sales, SALSA);

	return 0;
}

void showSold(int sales[], int salsa)
{
	for(int count = 0; count < salsa; count++)
	{
		cout << "The amount each salsa sold are:\n";
		cout << sales[count] << " ";
		cout << endl;
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.