Highest and lowest string in array

I have two problems that I am stuck on. First the total accumulator is all over the place. one time I run the program and get a correct answer and the next it is way off. Second how do I print the string that is parallel to the number that I get when I run the highest and lowest? And what am I doing wrong? Every time I run this I get the last element of each array. Here is what I have so far.

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
#include <iostream>
#include <string>
using namespace std;


int main()
{
	const int size = 5;
	string salsa[size] = {"Mild", "Medium", "Sweet", "Hot", "Zesty"};
	int sold[size];
	int total;
	string lowType, highType;
	int lowest, highest;

	cout<<"Enter the total number of jars sold for each type of salsa:\n";
	for (int count=0; count<size; count++)
	{
		cout<<"For "<<(salsa[count])<<endl;
		cin>>(sold[count]);
		while(sold[count]<0)
		{
			cout<<"Sales cannot be less than 0.\n";
			cin>>(sold[count]);
		}
		total+=(sold[count]);
	}
	for (int count=0; count<size; count++)
	{
		cout<<(salsa[count])<<" sold "<<(sold[count])<<" units last month.\n";
	}
	lowType=salsa[0];
	lowest=sold[0];
	for (int count=1; count<size; count++)
	{
		if (sold[count]<lowest) 
			lowest=sold[count];
			lowType=salsa[count];
	}
	highType=salsa[0];
	highest=sold[0];
	for (int count=1; count<size; count++)
	{
		if (sold[count]>highest)
			highest=sold[count];
			highType=salsa[count];
	}
	cout<<"The total number of jars sold was "<<total<<endl;
	cout<<"The salsa with the lowest sales is "<<lowType<<endl;
	cout<<"The salsa with the highest sales is "<<highType<<endl;
	system("pause");
	return 0;
}
You forgot {}s around lines 36, 37 and 44, 45. By the way, ()s when dealing with cin and cout are not needed as []s have higher precedence than >> and <<. If you're ever in doubt, find yourself an operator precedence table, to avoid silly ()s.

As for total, it is never set to 0, so could hold any garbage value.
Last edited on
Facepalm... Something that simple. The high/low is now working perfect, but the total still jumps around. I just put 5,4,3,2,1 in and got 105 as an answer. Then ran it again with the same numbers and got 15. Any thoughts on what is causing this to happen?
You probably just made a typo somewhere when you were testing with that input.
(You probably actually entered something like 55, 44, 3, 2, 1, which gives a total of 105)

Or at least I'm consistently getting 15 for the total with 5,4,3,2,1 as input.
I wish it was that simple. I know I am not doing any typos. I have tried several other numbers as well and the same problem happens. If you are getting consistent answers then maybe there is a problem with Visual Studios Express that I am using.
If you are getting consistent answers then maybe there is a problem with Visual Studios Express that I am using
.

Maybe. Or maybe it's because you never initialize total and it contains random junk.

I see that you are calculating total

total+=(sold[count]);

but you did not specify initial value for it. When you are defining a local variable such a way as

int total;

when its value is undefined.

So either define the variable as

int total = 0;

or assign it value somewhere after its definition and before its using

total = 0;
I see what I did wrong. Thanks for the help.
Topic archived. No new replies allowed.