How to save an array from a function after executing it, and then use this array in another function?

I have this program, where I have to choose an option 1-5, and the first option is to make an array with X amount of numbers and type them in the terminal in a function and then save it, so other functions can use this array.

For now, I have done the functions, but when I call the other functions they call the first one and the first function starts all over (asks for the number) and I can’t save the array in main() to give it to the other functions.

I have tried pointers from the array in the first function to the others, it doesn’t work, it again calls the first function from the 2nd or 3th.

#include <iostream>
#include <list>
using namespace std;
 
int* addCollegeNums()
{
    int num;
    cout << "\nhow many students are there \n";
    while (num < 1 || num > 25)
    {
        cin >> num;
        if (num < 1)
        {
            cout << "Not enough students! \n";
        }
        else if (num > 25)
        {
            cout << "Too many students!";
        }
    }
    cout << "give me there college numbers\n";
    static int* collegenums = new int[25];
    for (size_t i = 0; i <= 24; i++)
    {
        collegenums[i] = 0;
    }
    for (int i = 0; i <= num - 1; i++)
    {
        cin >> collegenums[i];
        if (collegenums[i] < 23621130 || collegenums[i] > 23621155)
        {
            cout << "invalid number\n";
            i--;
        }
        else
        {
            int samenum = collegenums[i];
            for (int j = 0; j <= i; j++)
            {
                if (i == j)
                {
 
                }
                else if(samenum == collegenums[j])
                {
                    cout << "this number already exists\n";
                    i--;
                }
            }
        }
    }
    return collegenums;
}
 
int evenCollegeNums()
{
    int * collegenums = addCollegeNums();
    int evencollegenumsArray[25];
    int trueEvenNum = 0;
    for (size_t i = 0; i < 24; i++)
    {
        evencollegenumsArray[i] = 1;
    }
 
    for (size_t i = 0; i <= 24; i++)
    {
        if (collegenums[i] % 2 == 0)
        {
            evencollegenumsArray[trueEvenNum] = collegenums[i];
            trueEvenNum++;
        }
    }
    int minnum = evencollegenumsArray[0];
    for (size_t i = 0; i < 24; i++)
    {
        if (evencollegenumsArray[i] == 1)
        {
            break;
        }
        else if(minnum > evencollegenumsArray[i])
        {
            minnum = evencollegenumsArray[i];
        }
    }
    return minnum;
}
 
int *bSortCollegeNums()
{
    int * collegenums = addCollegeNums();
    int *bSortArry = new int[25];
    for (size_t i = 0; i <= 24; i++)
    {
        bSortArry[i] = collegenums[i];
    }
    int x;
    for (size_t j = 0; j < 24; j++)
    {
        for (size_t i = 0; i < 24; i++)
        {
            if (bSortArry[i + 1] == 0)
            {
                break;
            }
            else
            {
                if (bSortArry[i] > bSortArry[i + 1])
                {
                    {
                        x = bSortArry[i];
                        bSortArry[i] = bSortArry[i + 1];
                        bSortArry[i + 1] = x;
                    }
                }
            }
        }
    }
    return bSortArry;
}
 
int executeFunctions()
{
    int * collegenums = addCollegeNums();
    cout << "\nThese are all College Numbers: \n";
    for (size_t i = 0; i < 24; i++)
    {
        if (collegenums[i] == 0)
        {
            break;
        }
        else
        {
            cout << collegenums[i] << " ";
        }
    }

    cout << "\nThe lowes, even College Number is: ";
    cout << evenCollegeNums(); 
 
    cout << "\nCollege Number in Ascending order: ";
    int * bSortArry = bSortCollegeNums();
    for (size_t i = 0; i < 24; i++)
    {
        cout << bSortArry[i] << " ";
    }
}
 
int main()
{
    cout << "Choose and option \n1.Input College Numbers of Students \n2.Take all of the Even College Numbers and Find the minimal College Number \n3.Sort the College Numbers in Ascending order \n4.Print out info for a chousan option \n5.Quit \n";
    int options;
    do
    {
        cout << "Option: ";
        cin >> options;
        if (options == 1)
        {
            addCollegeNums();
        }
        else if (options == 2)
        {
            evenCollegeNums();
        }
        else if (options == 3)
        {
            bSortCollegeNums();
        }
        else if (options == 4)
        {
            executeFunctions();
        }
        else if(options == 5)
        {
            string qornotq;
            cout << "are you sure Y/N";
            cin >> qornotq;
            if (qornotq == "Y" || qornotq == "y")
            {
                options = 5;
            }
            else
            {
                options = 0;
            } 
        }
        else
        {
            cout << "There isn't such and option as " << options << ". Please try again\n";
        }
    } while (options != 5);
}

  • 4

    why aren’t you using vector ?

    – 




  • 2

    or if the size is fixed std:.array<int, 25>

    – 

  • as a band-aid solution, you can use a static bool input_already_taken = false; then if (input_taken == true) return collegenums ; else you do the whole input thing before input_already_taken=true; return collegenums;, and the next time you call the function input_already_taken would be true and no input polling will happen.

    – 




There is no reason to call addCollegeNums() in all of the other functions. That is the root of your problem.

main() should be saving the int* pointer that addCollegeNums() returns when it calls that function, and then pass that pointer to the other functions when calling them.

Also, there is no need to allocate collegenums dynamically using int[] at all since it is so small and has a fixed size.

Also, you are leaking the int[] array that bSortCollegeNums() returns to pointer to. You might consider using a static int[] array for that function, as well.

Try this instead:

...
 
int* addCollegeNums()
{
    ...
    static int collegenums[25];
    ...
    return collegenums;
}
 
int evenCollegeNums(int *collegenums)
{
    // use collegenums as needed...
}
 
int* bSortCollegeNums(int *collegenums)
{
    // use collegenums as needed...
    ...
    int *bSortArry = new int[25];
    // or: static int bSortArry[25];
    ...
    return bSortArry;
}
 
int executeFunctions(int *collegenums)
{
    // use collegenums as needed...
    ...
    int *bSortArry = bSortCollegeNums(collegenums);
    ...
    delete[] bSortArry; // if the array in bSortCollegeNums is static, don't do this
}
 
int main()
{
    ...
    int options;
    int *collegenums = nullptr;

    do
    {
        ...
        if (options == 1)
        {
            collegenums = addCollegeNums();
        }
        else if (options == 2)
        {
            if (collegenums)
                evenCollegeNums(collegenums);
            else
                cout << "Choose option 1 first\n" << endl;
        }
        else if (options == 3)
        {
            if (collegenums)
            {
                int *bSortArry = bSortCollegeNums(collegenums);
                ...
                delete[] bSortArry; // if the array in bSortCollegeNums is static, don't do this
            }
            else
                cout << "Choose option 1 first\n" << endl;
        }
        else if (options == 4)
        {
            if (collegenums)
                executeFunctions(collegenums);
            else
                cout << "Choose option 1 first\n" << endl;
        }
        else if (options == 5)
        {
            ...
        }
        else
        {
            ...
        }
    } while (options != 5);
}

An alternative approach is to remove the collegenums array from addCollegeNums() altogether and move it into main() instead, and then pass it into addCollegeNums() to be filled (same with bSortCollegeNums()), eg:

...
 
void addCollegeNums(int *collegenums)
{
    // fill collegenums as needed...
}
 
int evenCollegeNums(int *collegenums)
{
    // use collegenums as needed...
}
 
void bSortCollegeNums(int *collegenums, int *bSortArry)
{
    // use collegenums and fill bSortArry as needed...
}
 
int executeFunctions(int *collegenums)
{
    // use collegenums as needed...
    ...
    int bSortArry[25];
    bSortCollegeNums(collegenums, bSortArry);
    ...
}
 
int main()
{
    ...
    int options;
    int collegenums[25] = {};
    bool collegenumsFilled = false;

    do
    {
        ...
        if (options == 1)
        {
            addCollegeNums(collegenums);
            collegenumsFilled = true;
        }
        else if (options == 2)
        {
            if (collegenumsFilled)
                evenCollegeNums(collegenums);
            else
                cout << "Choose option 1 first\n" << endl;
        }
        else if (options == 3)
        {
            if (collegenumsFilled)
            {
                int bSortArry[25];
                bSortCollegeNums(collegenums, bSortArry);
                ...
            }
            else
                cout << "Choose option 1 first\n" << endl;
        }
        else if (options == 4)
        {
            if (collegenumsFilled)
                executeFunctions(collegenums);
            else
                cout << "Choose option 1 first\n" << endl;
        }
        else if (options == 5)
        {
            ...
        }
        else
        {
            ...
        }
    } while (options != 5);
}

Leave a Comment