Help with parallel arrays

I'm trying to complete this program, but I'm having issues. Here is the problem I'm working on:

Write a program that lets a maker of chips and salsa keep track of sales for five different types of salsa: mild, medium, sweet, hot, and zesty. The program should use two parallel 5-element arrays: an array of strings that holds the five 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 this 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. Do not accept negative values for number of jars sold.

With the code I have written so far, the program works properly up to the point where I need to display the names of the highest and lowest selling items. My program works correctly occasionally, but sometimes it displays the wrong names and other times it shuts down prematurely. It depends on which values you enter for sales and the order in which you enter them. Any advice is appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
 #include <iostream>
#include <string>

using namespace std;



int main()

{
	
	const int Flavors = 5;
	string name[Flavors] = {"mild", "medium", "sweet", "hot", "zesty"};
	int sales[Flavors];
	
	for (int count = 0; count < Flavors; count++)
	{
		cout << "Enter number of jars sold of " << name[count] << ": ";
		cin >> sales[count];
		while (sales[count] < 0)
		{
			cout << "Please enter a valid integer of zero or greater: ";
			cin >> sales[count];
		}
	}
	
	cout << "\n\nSales Report\n";
	cout << "-------------\n\n";
	for (int count = 0; count < Flavors; count++)
	{
		cout << name[count] << ": " << sales[count] << endl;
		
	}
	
	int total = 0;
	for (int count = 0; count < Flavors; count++)
	{
		total+=sales[count];
	}
	
	cout << "\nTotal Sales: " << total << endl;
	
	int highest;
	highest = sales[0];
	for (int count = 0; count < Flavors; count++)
	{
		if (sales[count] > highest)
			highest = count;
	}
	
	cout << "\nThe highest selling salsa is " << name[highest] << endl;
	
	int lowest;
	lowest = sales[0];
	for (int count = 0; count < Flavors; count++)
	{	
		if (sales[count] < lowest)
			lowest = count;
	}
	
	cout << "The lowest selling salsa is " << name[lowest];
	
	
	
		
	
	return 0;
	
}

Should the variables highest and lowest contain the number of sales or the index number?
Line 43 to 49: You sometimes use variable highest as an index into sales array and another time as sales value. If sales[0] is the highest value in your array and if this is greater than Flavours you may get a crash in line 51. The same with lowest in lines 53 to 61.
Peter - I believe they should contain the index number. The end goal is to display the name of the highest and lowest selling items, not the number of sales of the highest and lowest. So my attempt was to try to get the "highest" and "lowest" variables to be the same value as the "location" in the array that corresponds to those names. Sorry if my terminology is off - I'm very new to all of this.

tcs - You seem to have nailed the problem. When I enter decreasing values into the array with sales[0] being 5 or greater, it crashes when it reaches line 51. I'm trying to wrap my head around your explanation for it.

Line 44 assigns the value of sales[0] to my variable "highest", correct? If that value is higher than the other values in the sales array, then line 48 never executes. So if my sales[0] value is higher than the number of Flavors, there is no corresponding "location" in my Flavors array. Am I on the right track?

Can you (or anyone) possibly offer a suggestion of a better way to do this? Thanks for the help so far. It is much appreciated.
1
2
3
4
5
6
7
8
	int highest = 0;
	for (int count = 1; count < Flavors; count++) // Start with 1 because sales[0] > sales[0] will ever be false
	{
		if (sales[count] > sales[highest])
			highest = count;
	}
	
	cout << "\nThe highest selling salsa is " << name[highest] << endl;
Now that I see it typed out, it seems so easy... Thanks again. I really appreciate it.
Topic archived. No new replies allowed.