Arrays and Functions

Dec 10, 2010 at 9:13pm
I have a mess of code and can't figure out what I'm doing wrong. I'm still fairly new to C++ and just when I think I've got a grasp on it...BAM! I've forgotten everything. If one of the gurus can figure this out, any help 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// This program produces a sales report for a salsa maker who
// markets 5 types of salsa. It includes total sales for all
// products and identifies the highest and lowest selling product.

#include<iostream>
#include <iomanip>
using namespace std;

// Function prototypes
int getTotal(int [], int);
int posOfHighest(int[], int);
int posOfSmallest(int [], int);

int main()
{
   const int NUM_TYPES = 5;   
   
   // Create the arrays for the names and sales amounts
   string name[NUM_TYPES] = {"mild", "sweet", "medium", "hot", "zesty"};
   int sales[NUM_TYPES];
   
   int totalJarsSold,
       hiSalesProduct,
       loSalesProduct,
	   totalSales;
     
   // Input the number of jars sold

	for (int type = 0; type < NUM_TYPES; type++)
	{
		cout << "Jar sold last month of " << name[type] << ": ";
      	cin  >> sales[type];
      
		while (sales[type] < 0)
		{	cout << "Jars sold must be 0 or more.  Please re-enter: ";
		   cin  >> sales[type];
		}
	}
   
   // Call functions to get total sales and high and low selling products
   totalJarsSold  = getTotal(sales, NUM_TYPES);
   hiSalesProduct = posOfHighest(sales, NUM_TYPES);
   loSalesProduct = posOfSmallest(sales, NUM_TYPES);
   
   // Produce the sales report
   cout << endl << endl;
   cout << "     Salsa Sales Report \n\n";
   cout << "Name              Jars Sold \n";
   cout << "____________________________\n";
   
   for (int salsaType = 0; salsaType < NUM_TYPES; salsaType++)
      // insert the code that prints the salsa names and number jars sold
	  totalSales = getTotal(sales, NUM_TYPES);
   
   cout << "\nTotal Sales:" << setw(15) << totalJarsSold << endl;
   cout << "High Seller: "  << name[hiSalesProduct] << endl;
   cout << "Low Seller : " << name[loSalesProduct] << endl;    
        
   return 0;
}

/************************************************************
 *                       	getTotal                        *
 * Calculates and returns the total of the values stored in *
 * the array passed to the function.                        *
 ************************************************************/
int getTotal(int array[] , int)
{
	int totalSales = 0;

	for (int count = 0; count < sales; sales++)
		totalSales += sales[count];
	return totalSales;
}

/************************************************************
 *                    posOfLargest                          *
 * Finds and returns the subscript of the array position    *
 * holding the largest value in the array passed to the     *
 * function.                                                *
 ************************************************************/
int posOfHighest(int array[], int numElts)
{
	int indexOfHighest = 0;

	for (int pos = 1; pos > numElts; pos++)
	{
		if(array[pos] > array[indexOfHighest])
			indexOfHighest = pos;
	}
	return indexOfHighest;
}

/************************************************************
 *                    posOfSmallest                         *
 * Finds and returns the subscript of the array position    *
 * holding the smallest value in the array passed to the    *
 * function.                                                *
 ************************************************************/
int posOfSmallest(int array[], int numElts)
{
	int indexOfSmallest = 0;
	
	for (int pos = 1; pos < numElts; pos++)
	{
		if (array[pos] < array[indexOfSmallest])
			indexOfSmallest = pos;
	}		
	return indexOfSmallest;
}


Thanks in advance for any advice and help.
Dec 10, 2010 at 9:21pm
What are you having trouble with?
Dec 11, 2010 at 10:47am
I'm lost and don't know where to go from here. The prog. doesn't work.
Dec 12, 2010 at 7:47am
No takers? Help?
Dec 12, 2010 at 7:13pm
Read my comments, marked with a $.
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// This program produces a sales report for a salsa maker who
// markets 5 types of salsa. It includes total sales for all
// products and identifies the highest and lowest selling product.

#include<iostream>
#include <iomanip>
using namespace std;

// Function prototypes
int getTotal(int [], int);
int posOfHighest(int[], int);
int posOfSmallest(int [], int);

int main()
{
   const int NUM_TYPES = 5;   
   
   // Create the arrays for the names and sales amounts
   string name[NUM_TYPES] = {"mild", "sweet", "medium", "hot", "zesty"};
   int sales[NUM_TYPES];
   
   int totalJarsSold,
       hiSalesProduct,
       loSalesProduct,
	   totalSales;
     
   // Input the number of jars sold

	for (int type = 0; type < NUM_TYPES; type++)
	{
		cout << "Jar sold last month of " << name[type] << ": ";
      	cin  >> sales[type];
      
		while (sales[type] < 0)
		{	cout << "Jars sold must be 0 or more.  Please re-enter: ";
		   cin  >> sales[type];
		}
	}
   
   // Call functions to get total sales and high and low selling products
   totalJarsSold  = getTotal(sales, NUM_TYPES);
   hiSalesProduct = posOfHighest(sales, NUM_TYPES);
   loSalesProduct = posOfSmallest(sales, NUM_TYPES);
   
   // Produce the sales report
   cout << endl << endl;
   cout << "     Salsa Sales Report \n\n";
   cout << "Name              Jars Sold \n";
   cout << "____________________________\n";
   
   for (int salsaType = 0; salsaType < NUM_TYPES; salsaType++)
      // insert the code that prints the salsa names and number jars sold
	  totalSales = getTotal(sales, NUM_TYPES);
   
   cout << "\nTotal Sales:" << setw(15) << totalJarsSold << endl;
   cout << "High Seller: "  << name[hiSalesProduct] << endl;
   cout << "Low Seller : " << name[loSalesProduct] << endl;    
        
   return 0;
}

/************************************************************
 *                       	getTotal                        *
 * Calculates and returns the total of the values stored in *
 * the array passed to the function.                        *
 ************************************************************/
int getTotal(int array[] , int count)//$you didn't name the second argument
{
	int total = 0;//$meant to be general, right.

	for (int i = 0; i < count; i++)//$this for loop was really screwed up.
		total += array[i];
	return total;
}

/************************************************************
 *                    posOfLargest                          *
 * Finds and returns the subscript of the array position    *
 * holding the largest value in the array passed to the     *
 * function.                                                *
 ************************************************************/
int posOfHighest(int array[], int numElts)
{
	int indexOfHighest = 0;

	for (int pos = 1; pos > numElts; pos++)
	{
		if(array[pos] > array[indexOfHighest])
			indexOfHighest = pos;
	}
	return indexOfHighest;
}

/************************************************************
 *                    posOfSmallest                         *
 * Finds and returns the subscript of the array position    *
 * holding the smallest value in the array passed to the    *
 * function.                                                *
 ************************************************************/
int posOfSmallest(int array[], int numElts)
{
	int indexOfSmallest = 0;
	
	for (int pos = 1; pos < numElts; pos++)
	{
		if (array[pos] < array[indexOfSmallest])
			indexOfSmallest = pos;
	}		
	return indexOfSmallest;
}

There ya go, fixed.
Dec 12, 2010 at 8:38pm
OK, I see that mistake and I get it. However, when I make the corrections and try to run, I get the following error:

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion).

???? I've never seen that one before.
Dec 12, 2010 at 9:37pm
what compiler are you using? I can compile and run the code above, however there may be another error:
Jar sold last month of mild: 3
Jar sold last month of sweet: 3
Jar sold last month of medium: 2
Jar sold last month of hot: 4
Jar sold last month of zesty: 5


     Salsa Sales Report 

Name              Jars Sold 
____________________________

Total Sales:             17
High Seller: mild
Low Seller : medium
Last edited on Dec 12, 2010 at 9:39pm
Dec 12, 2010 at 11:59pm
I'm using Visual Studio 2008.
Dec 13, 2010 at 12:40am
you need #include <string>
Dec 13, 2010 at 2:11am
OMG! That's so easy. I can't believe I forgot that. Thank you so much. It worked like a charm!
Topic archived. No new replies allowed.