Passing Arrays of Objects to Functions

I have an array of objects and I am using this array in 3 separate function calls: totalJars, highest, and lowest. Everything compiles without error, but I am having problems with 2 of the functions. The totalJars function works correctly, but I can't get the highest and lowest functions to work. As the code is currently written, the highest function returns a '-', and the lowest function doesn't return anything at all. If someone could tell me what I am missing I would greatly appreciate it.

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <string>
#include <iomanip>
#include "Jars.hpp"

//Function prototypes
int totalJars(Jars salsa[], int);
int highest(Jars salsa[], int);
int lowest(Jars salsa[], int);

int main()
{
   const int NUM_TYPES = 5;
   Jars salsaData[NUM_TYPES];

   int total;

   std::string highestSales;
   std::string lowestSales;
   std::string dashes;

   //Gets input from user and stores in array

   for(int i = 0; i < NUM_TYPES; i++)
   {
        std::string salsaName;
        int salsaQuantity;

        std::cout << "\nSalsa Type " << (i + 1) << ":  " << std::endl;
        std::cout << "Name:  ";
        std::cin >> salsaName;
        salsaData[i].setName(salsaName);
        std::cout << "Jars Sold:  ";
        std::cin >> salsaQuantity;
        salsaData[i].setQuantity(salsaQuantity);
   }
   std::cout << std::endl << std::endl;

   //Calls member functions to calculate total and highest and lowest sales
   total = totalJars(salsaData, NUM_TYPES);
   highestSales = highest(salsaData, NUM_TYPES);
   lowestSales = lowest(salsaData, NUM_TYPES);

   //Prints Report
   std::cout << "\n\n         Company Sales\n";
   dashes.assign (30, '-');
   std::cout << dashes << std::endl;

   for(int d = 0; d< NUM_TYPES; d++)
   {
        std::cout << "Salsa Type " << (d + 1) << ":  ";
        std::cout << salsaData[d].getName() << std::endl;
        std::cout << "   Jars Sold:  ";
        std::cout << salsaData[d].getQuantity() << std::endl;
        std::cout << std::endl;
   }
   std::cout << std::endl;

   std::cout << "Highest Sales:    " << highestSales << std::endl;
   std::cout << "Lowest Sales:     " << lowestSales << std::endl << std::endl;

   return 0;
}

/*************************************************
 *                                              *
 *              totalJars                       *
 *                                              *
*************************************************/
int totalJars(Jars salsa[], int nums)
{
   int tot = 0;
   int q;

   for(int i = 0; i < nums; i++)
   {
        q = salsa[i].getQuantity();
        tot += q;
   }

   return tot;
}

/*************************************************
 *                                              *
 *              highest                         *
 *                                              *
*************************************************/
int highest(Jars salsa[], int nums)
{
   int high = 0;
   int q;

   for (int i = 0; i < nums; i++)
   {
        q = salsa[i].getQuantity();
        if(q > high)
        {
           high = q;
        }
   }

   return high;
}

/*************************************************
 *                                              *
 *              lowest                          *
 *                                              *
*************************************************/
int lowest(Jars salsa[], int nums)
{
   int low = 0;

   for (int i = 1; i < nums; i++)
   {
        if (salsa[i].getQuantity() < low)
        {
           low = salsa[i].getQuantity();
        }
   }

   return low;
}


highest and lowest are defined to return int. However, you are trying to put the result in string variables.
Declare highestSales and lowestSales to be int, not string.

lowest() is also wrongly coded. Initialise low with the first value.
Last edited on
Thanks so much!!
Topic archived. No new replies allowed.