-1

So for my problem I need to have a dynamically allocated array that is to be created in the main function and populated in another function. The issue I'm having is that I then need to use that array in other functions and my array has no value after I populate it in my function (or at least this seems to be the case) Here is my code:

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

//prototypes
int getNumber();
void getMovieData(int *ptrToArray, int arraySize);
void sort(int *ptrToArray, int arraySize);
double getAverage(int *ptrToArray, int arraySize);
void print(int *ptrToArray, int arraySize);

int main()
{
    int stuNum = 0;
    int* stuArray;
    stuArray = new int[stuNum];

    getMovieData(stuArray, stuNum);

    cout << "--- Here is the data you entered ---" << endl;
    print(stuArray, stuNum);

    sort(stuArray, stuNum);
    cout << "--- Here is the data you entered sorted ---" << endl;
    print(stuArray, stuNum);

    cout << fixed << setprecision(2);
    cout << "Here is the average of your survey" << getAverage(stuArray, stuNum) << endl;


    system("pause");
    return 0;
}

int getNumber()
{
    int userNum;
    cin >> userNum;
    while (userNum <= 0)
    {
        cout << "Error number must be greater than zero." << endl;
        cin >> userNum;
    }
    return userNum;
}

void getMovieData(int *ptrToArray, int arraySize)
{
    cout << "Enter the number of students being surveyed: ";
    arraySize = getNumber();
    for (int i = 0; i < arraySize; i++)
    {
        cout << "Enter the movies seen by Student " << i + 1 << ": ";
        ptrToArray[i] = getNumber();
    }
    return;
}

void sort(int *ptrToArray, int arraySize)
{
    for (int i = 0; i < arraySize; i++)
    {
        for (int j = 0; j < arraySize - 1; j++)
        {
            if (ptrToArray[j] > ptrToArray[j + 1])
            {
                int temp = ptrToArray[j];
                ptrToArray[j] = ptrToArray[j + 1];
                ptrToArray[j + 1] = temp;
            }
        }
    }
}

double getAverage(int *ptrToArray, int arraySize)
{
    int total = 0;
    for (int i = 0; i < arraySize; i++) { total = total + ptrToArray[i]; }
    return total;
}

void print(int *ptrToArray, int arraySize)
{
    for (int i = 0; i < arraySize; i++) { cout << ptrToArray[i] << "\t"; }
    cout << endl;
}
Ethee
  • 11
  • 1
  • 2
    Get rid of the habit to allocate a buffer and pass that (with the size) to a function - Have an initial function returning a std::vector and pass that by reference to other functions for further processing. –  Apr 23 '14 at 23:22
  • You can see this answer:http://stackoverflow.com/questions/1087042/c-new-int0-will-it-allocate-memory – BlackMamba Apr 23 '14 at 23:43
  • Zero sized arrays are not allowed in standard C++ but are sometimes implementation defined, and dynamically allocating an array when you know the number of elements at compile-time is just plain weird, ESPECIALLY when that compile time known variable is 0. The standard says that you can dynamically allocate an array of zero length, but never dereference(use) it. – RamblingMad Apr 23 '14 at 23:53
  • Why the close and down votes? Seems like a reasonable beginners question to me, well stated with enough code to demonstrate the issue. – dkackman Apr 24 '14 at 00:36

1 Answers1

4

You are allocating an array with zero elements. Change the value of stuNum to a positive number representing the number ints you need.

dkackman
  • 14,361
  • 11
  • 63
  • 118
  • stuNum gets changed later before the array becomes populated so the initialization `stuNum = 0;` shouldn't be an issue right? – Ethee Apr 23 '14 at 23:19
  • @Ethee: The _pointer_ is "populated" at `new int[stuNum]`, and there, `stuNum` is 0.\ – Mooing Duck Apr 23 '14 at 23:20
  • @Ethee I see what you want to do but you're doing it wrong. You want the user to enter the number of elements and then allocate an array for them. Only *after* asking for the size you can allocate your array by doing `new int[size]`. You allocate *before* calling `getMovieData` where that size is 0. You want that allocation to happen in the function. But then you have to provide both pointer and array size as references so the changes are visible to the outer code in main. – leemes Apr 23 '14 at 23:21
  • 2
    Or better: use `std::vector` instead. – leemes Apr 23 '14 at 23:22
  • I see what the issue is now, but unfortunately I have to allocate in the main() function as according to the specifications I got. So now I feel this is impossible. – Ethee Apr 23 '14 at 23:24
  • @Ethee: In your specifications, as you've described them so far, I don't see where it says you need to ask the user to input the number of students in the same function which populates the data of the array. So you should be able to do that *outside* of the function, *before* you allocate the array. – Benjamin Lindley Apr 23 '14 at 23:26
  • @BenjaminLindley Thank you for that, I had to double check to make sure that was true. And I do believe I have fixed my problem now so thank you guys. – Ethee Apr 23 '14 at 23:29
  • You could call one function to prompt and get the number of students, then allocate the array, then call another function to populate the array. – rcgldr Apr 23 '14 at 23:30