So I have to make a bank program for an assignment, and my professor wants a log file of every "operation"(He was really vague about that, I decided to log inputs and outputs). The issue I'm having though is that every time I call a new function, I have to reopen the file and it overwrites what was previously there. As far as I can tell, using .open repeatedly will cause whatever function I'm in to ignore the previous output.
I tried declaring a global ofstream hoping that it would change, but the issue persists either way. Flushing and/or closing didn't seem to help,but it's entirely possible I misused them or the syntax was wrong. The code below is the main function, read_accts function, and menu function. If I terminate the program before calling the read_accts function, the log file will have "How many accounts are there?" But if I allow the program to call the other two functions, then the log file only has the output of the menu file. I apologize for the long post, but I'm at a loss as to what's going wrong.
int main()
{
ofstream log;
log.open("Log.txt");
int MAX_NUM = 100;
BankAccount account[MAX_NUM];
int num_accts = 0;
char selection = 'Z';
cout << "How many accounts are there?" << endl;
log << "How many accounts are there?" << endl;
cin >> MAX_NUM;
read_accts(account, MAX_NUM, num_accts);
cout << endl;
cout << "There are " << num_accts << " accounts" << endl;
log << "There are " << num_accts << " accounts" << endl;
cout << endl;
print_accts(account, MAX_NUM);
cout << endl;
`
while (selection != 'Q' && selection != 'q')
{
menu();
cin >> selection;
log << selection;
switch (selection)
{
case 'W':
case 'w':
withdrawal(account, num_accts);
break;
case 'D':
case 'd':
deposit(account, num_accts);
break;
case 'N':
case 'n':
new_acct(account, num_accts);
break;
case 'B':
case 'b':
balance(account, num_accts);
break;
case 'I':
case 'i':
account_info(account, num_accts);
break;
case 'C':
case 'c':
close_acct(account, num_accts);
break;
case 'Q':
case 'q':
print_accts(account, num_accts);
break;
default:
cout << "Invalid selection" << endl;
log << "Invalid selection" << endl;
}
cout << endl;
}
print_accts(account, MAX_NUM);
cout << "Goodbye" << endl;
log.close();
return 0;
}
int read_accts(BankAccount account[], int MAX_NUM, int &num_accts)
{
ofstream log;
log.open("log.txt", std::ofstream::app);
string f;
string l;
int social;
int acct;
string type;
double bal;
int i = 0;
ifstream readfile;
readfile.open("bankdatain.txt");
if (!readfile)
{
cout << "Can't open input file." << endl;
log << "Can't open input file." << endl;
exit(1);
}
while (readfile >> f >> l >> social >> acct >> type >> bal)
{
account[i].setfname(f);
account[i].setlname(l);
account[i].setssnum(social);
account[i].setacctnum(acct);
account[i].settype(type);
account[i].setbalance(bal);
i++;
num_accts++;
}
return num_accts;
}
void menu()
{
ofstream log;
log.open("Log.txt");
cout << "W - Withdrawal" << endl;
log << "W - Withdrawal" << endl;
cout << "D - Deposit" << endl;
log << "D - Deposit" << endl;
cout << "N - New account" << endl;
log << "N - New account" << endl;
cout << "B - Balance" << endl;
log << "B - Balance" << endl;
cout << "I - Account Info" << endl;
log << "I - Account Info" << endl;
cout << "C - Close Account" << endl;
log << "C - Close Account" << endl;
cout << "Q - Quit" << endl;
log << "Q - Quit" << endl;
cout << "Please make your selection: " << endl;
log << "Please make your selection: " << endl;
}