0

This is the function that reads the file and I have tested it and it worked.

int readAccts(string acct[maxAccounts], int pin[maxAccounts], double bal[maxAccounts], int & numAccts)
{
   ifstream fin;
   fin.open("accounts.txt");
   if (fin.fail())
   {
     cout << "Error in reading accounts!" << endl;
     return -1;
   }

   //string acct, pin, bal;
   fin >> numAccts;
   cout << "num=" << numAccts << endl;
   for (int i = 0; i < numAccts; i++)
   {
       fin >> acct[i] >> pin[i] >> bal[i];
       printAccts();
   }
}

This code is suppose to print the read file and is invoked in the read function

int printAccts()
{
    for (int i = 0; i < numAccts; i++)
    {
        cout << acct[i] << pin[i] << bal[i] << endl;
    }
}

This is the code that invokes the read function (printLogo works)

void startUp()
{
    printLogo();
    cout << "Starting up INSTA-ATM..." << endl;
    readAccts();
}

text file

4
MB-1111-1111 1111 222.22
MB-2222-2222 2222 333.33
B1-3333-3333 3333 444.44
4444 4444 555.55

I am not sure what to put in for the arguments when I invoke the functions any help would be greatly appreciated.

  • Well, to begin, you're asking in your ```readAccts``` for a reference of an int for ```numAccounts```. This essentially passes in a memory address. In your code, you do not dereference this integer memory address, so that may be the problem. It may not be the only problem, however. Could you provide the function that calls the ```readAccts()``` function? – dddJewelsbbb Mar 09 '18 at 03:17
  • 2
    Unrelated: Save yourself some unnecessary book-keeping and build `string acct[maxAccounts], int pin[maxAccounts], double bal[maxAccounts]` into a single array of a structure that contains `acct`, `pin` and `bal`. Also consider making `bal` an integer that counts in pennies (or whatever your locale uses for its smallest monetary unit). `double`s are imprecise and people get really upset when that imprecision works against them. – user4581301 Mar 09 '18 at 03:27
  • @dddJewelsbbb void startUp should be the function that calls readAccts( ). I am just not sure what arguments are suppose to be used when I call it. – lord_Novision Mar 09 '18 at 03:40
  • @Noah okay, so you need arguments for that method call. I would suggest reading up on how to properly pass arrays as arguments in C++, which can be viewed [here](https://www.tutorialspoint.com/cplusplus/cpp_passing_arrays_to_functions.htm). You'll still need some arrays to pass into the function, along with a number telling you how many accounts there are to read. Your function argument signature tells you what is expected to be passed into the argument. – dddJewelsbbb Mar 09 '18 at 03:54
  • A very minimally fixed version might look like [this](https://wandbox.org/permlink/ApFwYJZpP7StmPoi). I would not recommend using the globals though instead pass the data between functions and things will be much improved. Follow @user4581301s advice and use classes to contain the account, pin and balance in an object. Those sized arrays require that arrays of exactly that size be passed to the function, for arrays the size of part of the type. If you dont want to have a fixed size use pointers, but then you need to pass a size. The best alternative is `std::array` or `std::vector`. – Paul Rooney Mar 09 '18 at 04:24
  • 2
    @dddJewelsbbb numAccts is a reference, not a pointer. References are automatically dereferenced. – eesiraed Mar 09 '18 at 04:28
  • @FeiXiang upvote for that. I actually never knew that – dddJewelsbbb Mar 09 '18 at 05:01
  • 1
    @dddJewelsbbb You can learn more [here](https://stackoverflow.com/q/57483/9254539). – eesiraed Mar 09 '18 at 05:06
  • @FeiXiang Thank you so much. It means a lot - always want to learn more! :) – dddJewelsbbb Mar 09 '18 at 05:07
  • Then let me tell you about `Nyarlathotep`... – user4581301 Mar 09 '18 at 18:31

0 Answers0