Using dynamic array function but can't figure out size

I know that a vector would solve all of my issues, but the chapter is on pointers and I have to make a dynamic array. The getSalesList function works as it should, but now i need the main function to actually iterate through the results. Problem is that I do not see a way to capture the array size for use with a 'for' loop, or any other way to make this work. Help is 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
#include<iostream>
using namespace std;

double* getSalesList();

int main()
{

   double *sales = nullptr; // declare sales pointer and init to null
   sales = getSalesList();

   //Want to put a for loop here to display array contents,
   //but I have no way to tell main the size of the array to iterate through

   delete[] sales;
   sales = nullptr;

   return 0;
}

double* getSalesList()
{
   int numDays = 0;
   double *array = nullptr;
   cout << "How many days of sales will you be entering? ";
   cin >> numDays;

   array = new double[numDays];

   for (int i = 0; i < numDays; i++)
   {
      cout << "Please enter the sales for position " << i + 1 << ": ";
      cin >> *(array + i);
      cout << endl;
   }

   return array;
}
make the getSalesList() return multiples values. the address of the array to the first element of the array AND the size of the array

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

void getSalesList(double*& , int&); //declare the function like this

int main()
{

   double *sales = nullptr; // declare sales pointer and int to null
   int arraySize = 0;
   getSalesList(sales, arraySize); // call the function like this


// use the arraySize in loop like this:
   for(int x = 0; x < arraySize; ++x)
   {
       cout << "Element " << x + 1 << ": " << sales[x] << endl;
   }

   delete[] sales;
   sales = nullptr;

   return 0;
}

 void getSalesList(double*& sales, int& arraySize)
{
   int numDays = 0;
   double *array = nullptr;
   cout << "How many days of sales will you be entering? ";
   cin >> numDays;

   array = new double[numDays];

   for (int i = 0; i < numDays; i++)
   {
      cout << "Please enter the sales for position " << i + 1 << ": ";
      cin >> *(array + i);
      cout << endl;
   }

   sales = array; // this changes affects the actual parameter passed
   arraySize = numDays; // this changes affects the actual parameter passed
}
Last edited on
Unless you encapsulate the array inside a structure you'll probably want to pass things as parameters into the function.
1
2
3
4
5
6
7
8
9
struct SalesData
{
   double *data;
   size_t size;
};

SalesData getSalesList();
double* getSalesList(size_t &size);
Thanks guys. Passing a ref parameter seems to be the most logical solution, however the question requires that a pointer to the array should be the return value of the getSalesList function.

Because of that, I am unable to pass another int to the func as a ref parameter. I seem to be stuck!

@jlb, a struct is something we have not covered yet, so I am unable to use it right now.
Because of that, I am unable to pass another int to the func as a ref parameter. I seem to be stuck!

Why not?

You can have multiple parameters of many different types but you can only have one return value.

Wow. I attempted that before even posting the question, but my syntax was off. Ugh. Sorry to waste everyone's time! Thanks for pushing me back to my original idea.

I appreciate all the help from everyone. Take care!
Topic archived. No new replies allowed.