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.
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.