Help for the highest

Apr 18, 2019 at 3:26pm
I'm trying to calculate the highest number and it is not letting me and it is giving me an error. r: invalid types 'std::__cxx11::string [5] {aka std::__cxx11::basic_string<char> [5]}[double]' for array subscript . I was wanting to know where I was messing up.

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;
double sumArray( const double [], int );
double getHighest( const double [], int );

int main()
{
const int SIZE = 5;
string name[SIZE] = {"mild", "medium","sweet", "hot", "zesty"};
double sales[SIZE], total, highest, lowest;

cout<<"Enter the salsa sales: "<<endl;
for (int i = 0; i < SIZE; i++)
{
	cout<<"Enter the amounts of "<<name[i]<<" sold: ";
	cin>>sales[i];
}

highest = getHighest(sales, SIZE);
total = sumArray(sales, SIZE);
cout<<"The total"<<total<<endl;
 cout<<name[highest];
return 0;


}

double sumArray(const double array[], int size)
{
	double total = 0;
	
	for(int count = 0; count < size; count++)
		total +=array[count];
	return total;
}

/* double getHighest(double array[], int size)
{
	
	int high = 0;
	
	for (int i = 1; i < size; i++)
	{
		if (array[i] > array[high])
			high = i;
	}
	return high;
	
} */

double getHighest(const double array[], int size)
{
	double highest = array[0];
	for (int count = 1; count < size; count++)
	{if(array[count]>highest)
		highest= array[count];
	}
	return highest;
}
Apr 18, 2019 at 3:35pm
Are you using a C++11 (or higher) compiler?

The only error I get when compiling against C++14 is at line 25,
C2108 subscript is not of integral type

An array subscript variable has to be an int (or short, long or long long), it can't be a double.

There is no array element numbered 1.36, or any other floating point number.
Apr 18, 2019 at 3:45pm
I'm using compiler C++11 but I can get the position of the highest. I were to go to cout<<highest instead of cout<<name[highest];. The question asks to get the name out of the highest but I get that error when I get the name out.
Apr 18, 2019 at 3:47pm
It's because highest is a double!

cout << name[3.1415926];
makes NO sense.

Make it an int.
Apr 18, 2019 at 3:54pm
Okay, I have done that it compiles but the output is not correct now, the name of the highest is not correct.
Apr 18, 2019 at 4:15pm
You need to return the index number of the highest selling salsa to use when retrieving the name:
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
#include <iostream>
#include <iomanip>
#include <string>

double sumArray(const double[], int);
int getHighest(const double[], int);

int main()
{
   const int   SIZE = 5;
   std::string name[] = { "mild", "medium","sweet", "hot", "zesty" };
   double      sales[SIZE];
   double      total;

   std::cout << "Enter the salsa sales:\n";
   for (int i = 0; i < SIZE; i++)
   {
      std::cout << "Enter the amounts of " << name[i] << " sold: ";
      std::cin >> sales[i];
   }

   int index = getHighest(sales, SIZE);
   total     = sumArray(sales, SIZE);

   std::cout << "\nTotal sales: " << total << '\n';
   std::cout << "Best selling salsa was " << name[index] << ".\n";
}

double sumArray(const double array[], int size)
{
   double total = 0;

   for (int count = 0; count < size; count++)
   {
      total += array[count];
   }

   return total;
}

int getHighest(const double array[], int size)
{
   double highest = 0;
   int    index = 0;

   for (int count = 1; count < size; count++)
   {
      if (array[count] > highest)
      {
         highest = array[count];
         index = count;
      }
   }

   return index;
}
Enter the salsa sales:
Enter the amounts of mild sold: 15
Enter the amounts of medium sold: 8
Enter the amounts of sweet sold: 22
Enter the amounts of hot sold: 8
Enter the amounts of zesty sold: 6

Total sales: 59
Best selling salsa was sweet.

Design question:

Why is the number of salsa (I presume some form of containers or cases of containers) sold a double? Can you sell a fraction? 3.48 jars/plastic tubs/boxes of salsa sold really doesn't make sense.
Topic archived. No new replies allowed.