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;
}
TheMrDrake
  • 11
  • 5
  • You don't delete `unique_ptr`'s. The whole point of these is that let them managing this. – πάντα ῥεῖ Feb 16 '15 at 18:10
  • 1
    @TheMrDrake please post a [Minimal. Complete, Verifiable Example](http://stackoverflow.com/help/mcve) – Jonathan Mee Feb 16 '15 at 18:13
  • 1
    @TheMrDrake Also what are `memory.h`, `ifstrteam.h`, `iomanip.h`, and `fstream.h`? These aren't standard header files. – πάντα ῥεῖ Feb 16 '15 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. – TheMrDrake Feb 16 '15 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](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead of asking about basics here. – πάντα ῥεῖ Feb 16 '15 at 18:23

1 Answers1

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.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • what if I want to do this without vectors? – TheMrDrake Feb 16 '15 at 18:22
  • @TheMrDrake _"what if I want to do this without vectors?"_ Why would you want this? Can you give any sensible reasoning for this restriction? – πάντα ῥεῖ Feb 16 '15 at 18:27
  • 1
    @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).. – Cheers and hth. - Alf Feb 16 '15 at 18:29
  • 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? – TheMrDrake Feb 16 '15 at 18:37