Hey guys, my first post here, so here is the problem:
I've worked on creating an ATM-like program, and now my only problem lies in writing the users balance to a text file. I can get the balance to write just fine, but when I start the program over, simulating revisiting an ATM, it resets the text in the file. I believe when I use membervar.open(filename) (for example), it is re-creating the file every time, although it already exists? Anyways, enough of my rambling.
void account()
{
transaction trans; // customer user has already been declared by now
ofstream ofile;
ifstream ifile;
int tran;
int amount;
int balance;
string continues;
string total;
do{
constchar * unam = (user.return_name() + ".txt").c_str();
ifile.open(unam);
cout << getline(ifile, total); // it has reset by this point: incorrect
balance = atoi(total.c_str());
trans.set_total(balance);
ifile.close();
ofile.open(unam); //each user has unique file for info/balance
cout << "\nwould you like to make a deposit(1) or withdrawal(2)?" << endl;
cin >> tran;
cout << "\nand how much?" << endl;
cin >> amount;
(tran == 1) ? trans.deposit(amount) : trans.withdraw(amount);
cout << trans.return_total(); //checking variable value, which is: correct
ofile << trans.return_total();
ofile.close();
Thanks for any help guys!! Also, although I didn't post all of my code, let me know how what has been posted can be improved!
Well when I run the program and either deposit or withdraw, then exit the program, that total is successfully written to the file desired. So, upon a second run of the program, what about the way I open the file, causes the file to be over written?
The first thing I do in the loop is read the contents of the file (which are no longer there) in to a variable, balance, which should be whatever was successfully written, above ^. Then after that the program gets to the writing portion. ?