Issue with low value, parallel arrays

This is a lab for intro to C++. The last line of code (Line 55) throws an exception. I cannot find my mistake here and my assumption is that it would work similar to the highSeller variable which works fine. Any input would be 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
#include<iostream>
#include<iomanip>
#include<string>

using namespace std; 

int main()
{
	string sauce[7] = { "mild", "medium", "hot","sweet", "fruit", "zesty", "verde" };
	int sales[7];
	int totalSales = 0;
	int i;
	int highSeller;
	int lowSeller;
	

	for (i = 0;i < sizeof(sales) / sizeof(sales[7]); i++)
	{
		cout << "Enter jars sold of " << sauce[i] << " this month: ";
		cin >> sales[i];
		totalSales += sales[i]; 


		while(sales[i] < 0)
			cout << "Please enter a value greater than zero.";

		
	}
	cout << "\t\tSalsa Sales Report" << endl << endl;
	cout << "Name" << "\t\t\tJars Sold" <<endl;

	for (i = 0; i < sizeof(sales) / sizeof(sales[7]); i++)
	{
		cout << sauce[i] << "\t\t\t" << sales[i] << endl;

	}
	highSeller = sales[0];
	if (sales[i] > sales[highSeller])
		highSeller = i;
	

	cout << "\nThe highest seller was: " << sauce[highSeller] << endl; 
	
	
	lowSeller = sales[0];
	if (sales[i] < sales[lowSeller])
		lowSeller = i;
	

	cout << "\nThe lowest seller was: " << sauce[lowSeller] << endl;
		
	 


	



	system("pause"); 
	
}
Hello mcnhscc39,

Although the program does compile there are many problems.

I would start "main" with:
1
2
3
4
5
6
int main()
{
	constexpr size_t MAXSIZE{ 7 };

	string sauce[MAXSIZE] = { "mild", "medium", "hot","sweet", "fruit", "zesty", "verde" };
	int sales[MAXSIZE]{ 100, 125, 75, 75, 50, 90, 100 };

By initializing "sales" with seven numbers you can comment out the first for loop and concentrate on the rest of the program.

On line 24 should the while condition become true it is an endless loop because the value of "sales[i]" never changes.

For line 32 you have for (i = 0; i < sizeof(sales) / sizeof(sales[7]); i++). This works, but this is much simpler and easier to read for (i = 0; i < MAXSIZE; i++).

Line 37 is OK, but line 38 is totally wrong."i" has a value of 7 which is one past the end of the array. There is no way of knowing what that value is. Its just garbage. I am not sure if you are aware of this, but a seven element array is numbered from zero to six.

Line 38 would work better as a for loop containing the if statement. It would also be better to start the for loop with for (int i = 0; ...). This way when you set "highSeller = i" "highSeller" will have the correct value. The same applies to the part about "lowSeller" although setting "lowSeller" to "sales[0]" may not always work.

When you get to lines 42 and 50 the subscript is likely to be past the end of your array. If this does not give you a run time error it will most likely be pringint garbage.

I will see what I can come up with.

Hope that helps,

Andy
Hello mcnhscc39,

I found this to work better:

1
2
3
4
5
6
7
highSeller = 0;
	
for (size_t lc = 0; lc < MAXSIZE; lc++)
{
	if (sales[lc] > sales[highSeller])
		highSeller = lc;
}

And the same when finding the lowest seller.

Hope that helps,

Andy
I caught line 24 and fixed that after I posted this, so that is good and functions as I wanted.

Line 37, I understand that array's start with 0 and so the last element is technically 6 but isn't it still 7 index's? I wasn't trying to number the array 0-7.

I will take a closer look and see if they problem is with my subscript exceeded my array size.

Thanks for your input.
also, it should be noted that the sales array is empty and is filled by user input. Not sure if that changes what your input.
So everything works with some of the changes except that mild is always listed as the highest seller regardless of whether it is or not.

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
72
73
74
75
76
77
78
79
80
81
#include<iostream>
#include<iomanip>
#include<string>

using namespace std;

int main()
{
	constexpr size_t MAXSIZE{ 7 };
	string sauce[MAXSIZE] = { "mild", "medium", "hot","sweet", "fruit", "zesty", "verde" };
	int sales[MAXSIZE];
	int totalSales = 0;
	int i;
	int highSeller;
	int lowSeller;


	for (i = 0; i < MAXSIZE; i++)
	{
		cout << "Enter jars sold of " << sauce[i] << " this month: ";
		cin >> sales[i];
		totalSales += sales[i];


		while (sales[i] <= 0)
		{
			cout << "Please enter a value greater than zero.";
			cin >> sales[i];
		}


	}
	cout << "\t\tSalsa Sales Report" << endl << endl;
	cout << "Name" << "\t\tJars Sold" << endl;

	
	for (i = 0; i < MAXSIZE; i++)
	{
		cout << sauce[i] << "\t\t\t" << sales[i] << endl;
	}

	cout << "Total jars sold: " << totalSales << endl; 
	highSeller = 0;
	
	for (size_t lc = 0; lc < MAXSIZE; lc++)
	{
		if (sales[lc] > sales[highSeller])
		{
			highSeller = lc;
		
		}
	}
		lowSeller = 1000;
	for (size_t lc = 0; lc < MAXSIZE; lc++)
	{
		if (sales[lc] < sales[lowSeller])
		{
			lowSeller = lc;
			
		}

		highSeller = 0;
	
	}

	cout << "\nThe highest seller was: " << sauce[highSeller] << endl;
	cout << "\nThe lowest seller was: " << sauce[lowSeller] << endl;


	








	system("pause");

}
The problem is when you are trying to find the "lowest seller".

Line 53: lowSeller = 1000; You don't have 1001 sauces.

Line 62: highSeller = 0; The "mild" sauce is the "highest seller".
Last edited on
Hello mcnhscc39,

Line 37, I understand that array's start with 0 and so the last element is technically 6 but isn't it still 7 index's? I wasn't trying to number the array 0-7.

Yes there are "7" indices " 0 - 6" "i" having the value of "7" means that there are "7" elements to the array. It can be a little confusing in the beginning until you get use to it.

In the if statement, line 38:
1
2
if (sales[i] > sales[highSeller])
		highSeller = i;

At this point "i" still has the value of 7, so sales[7] is one past the end of the array. in one run I had this value was "-858993460" and checking sales[100], way past the end of the array, gave me a number of "7370792". Not what you want to compare.

Remember that "highSeller" and "lowSeller are indices to an array not a number. That is why they start at zero.

This may be a long name, but "highSellerIndex" and "lowSellerIndex" would better describe what the variables are for and make it easier to understand what they do. I am not saying that yo need to change the names, its just something to think about for the future.

Not sure why "mild" is coming out as the high seller. So far every combination of numbers I have tried has worked properly.

I test you new version and see what happens.

Hope that helps,

Andy
Andy and Peter,

Everything seems to be working well now. Part of the problem is my confusion in thinking that the low and highSeller variables were independent from the arrays. I wasn't aware that they were referencing the index in the array.

Also the reason why mild was returning as the highest every time was a bit of code that was not removed from line 62 when I was editing. I removed it and everything worked.
Topic archived. No new replies allowed.