Given a History of transactions in a bank account how can we print an array of an account to a file? For example you call the function by its filename. Then list the information in each account (name,account number,balance) such as Account a1("bob","1234",199) account a2("Jim, "4567",345) .... account a20 ("Pete", "7890",400). Then in this example we would need an array length of 20 elements. I believe this has to do with file i/o and its an output file we must create.
Must also write a function that reads in a file showing a list of accounts and creates account objects out of the accounts and store valid accounts as objects in the accounts array, skip invalid accounts and, return the number of invalid objects. So far I have :
I need to write a function (void accounts2file(std::string ofname, Account accounts[], int numaccounts)) that prints an array of accounts to a file; The result should be something similar to this in the .txt file following this format.
N
accNum1 ss1 name1 bala1
accNum2 ss2 name2 bala2
...
...
accNumN ssN nameN balaN
The idea is to copy the string indexies from an array consisting of strings into a external .txt file to be "stored" as records.
I am not too good with file i/o and this is the question I was given. I know I have to write out to a file. I have a ".h" file and when calling it in my ".cpp" file it should write out to a file but I'm very confused on how to do this since we did not spend enough time on this topic.
@DTrey
A ".h" file is a header file, and I don't see how it could have anything to do with file input and output for your program.
You didn't declare numaccounts in the file2accounts function.
Line 30 should be inFile >> numaccounts;
You need a return numaccounts; at the end of the function.
Do you know how to create classes? Also, is the input file supposed to be the same format as the output file?
N
accNum1 ss1 name1 bala1
accNum2 ss2 name2 bala2
...
...
accNumN ssN nameN balaN
Yes this header file is where i created my class. I was given this function to implement and those were the directions. I know how to do file i/o for a basic code as opening a file that exists already or just writing out to a file. This has to do with file i/o and writing out to an array and its really confusing to me. The difficulty level is far greater than anything I have practiced so far and this is why its challenging.
Did not work. It actually made my program not compile. I'm not sure how to fix this issue because all the examples of codes written with file I/o are basically similar and when I try it doesn't work well with this program I've written.
How would I get it to display the number of accounts that are written to the txt file. For example:
2
Account 1 info
Account 2 info
I was able to see my error when I wrote these in my file and it compiles now. Would I have to outFile<<accounts[i] in the beginning to display the number of accounts that are written.