0

I am having trouble returning my two arrays that I dynamically allocate. I have an external file reading the size of the array. (20 in my case) and that is making the array size when I use the dynamically allocated arrays.

Also once I return them, is my current syntax correct or is there something that I should change.

Here is my code

int main (void)
{

    int size;
    int notFound = 0;
    int accountNumber = 0;
    int results;
    int * accountPtr = nullptr;
    double * balancePtr = nullptr;



    size = readFile(notFound, accountPtr, balancePtr);


    if (notFound == 0)
    {

     selectionSort(accountPtr, balancePtr, size);

        while  (accountNumber != -99)
            {
                cout << "Please Enter an Account Number (Type -99 to quit): ";
                cin >> accountNumber;

                if (accountNumber == -99)
                {
                    cout << "Goodbye!" << endl;
                }
                else
                {
                    results = binarySearch(accountPtr,accountNumber,size);
                    if ( results == -1)
                    {
                        cout << "That Account Number Does Not Exist." << endl;
                    }
                    else
                    {
                        cout << "\nAccount Number" << "\t\t" << "Account Balance" << endl;
                        cout << "-----------------------------------------------" << endl;
                        cout << fixed << setprecision(2) << accountPtr[results] << "\t\t\t" << balancePtr[results] << endl << endl;
                    }
                }


            }
    }

    return 0;
}

int readFile (int &notFound, int  *accountPtr, double  *balancePtr)

{
    int size;

    ifstream inputFile;

    inputFile.open("account.txt");

    if (inputFile.fail())
    {
     cout << "The File Account.TXT was not found." << endl;
     notFound = 1;
    }
    else
    {
        inputFile >> size;

        unique_ptr<int[]> accountPtr(new int[size]);
        unique_ptr<double[]> balancePtr(new double[size]);


        for(int i = 0; i < size; i++)
        {
            inputFile >> accountPtr[i] >> balancePtr[i];
        }

    }
    return size;
}
5
  • You don't delete unique_ptr's. The whole point of these is that let them managing this. Commented Feb 16, 2015 at 18:10
  • 1
    @TheMrDrake please post a Minimal. Complete, Verifiable Example Commented Feb 16, 2015 at 18:13
  • 1
    @TheMrDrake Also what are memory.h, ifstrteam.h, iomanip.h, and fstream.h? These aren't standard header files. Commented Feb 16, 2015 at 18:15
  • Is that better? I know its not returning my arrays "accountPtr" and balancePtr I create the new array in the same function just don't know how to return them so that other functions can use them. Also don't know how to set up function headers to use them. Commented Feb 16, 2015 at 18:19
  • @TheMrDrake Also: The simple solution instead of creating those "arrays" yourself (even using a unique_ptr), would be to use std::array or std::vector. "Also don't know how to set up function headers to use them." Better start with a good book instead of asking about basics here. Commented Feb 16, 2015 at 18:23

1 Answer 1

1

You're passing pointers by value. The pointer variables in the calling code will not be modified. In addition to passing by value, in the function you're declaring local variables of the same names as the formal arguments.

Instead you should be returning or passing by reference std::vector.

Sign up to request clarification or add additional context in comments.

4 Comments

what if I want to do this without vectors?
@TheMrDrake "what if I want to do this without vectors?" Why would you want this? Can you give any sensible reasoning for this restriction?
@TheMrDrake: When some teacher forbids using std::vector you essentially have to implement the subset of std::vector functionality that you're using. You might do that without encapsulation, by passing pointers by reference. It can be a learning experience, dealing with all the problems of that (it would not be a good idea to do this professionally)..
Can someone tell me how I can return the arrays after that first function is done? That is the only reason why my program is having problems. I make them in there use them but return size so that my other functions can use them. How do I return the arrays accountPtr and balancePtr?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.