File input issue, overwriting

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.

First, my relevant classes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

class customer{	
	private:
	string name;
	string ID;
	
	public:
	void setname(string nm){
		name = nm;
	}
	
	string return_name(){
		return name;
	}
};

class transaction{
private:
	int dollars;
	
public:
	string deposit(int money){
		dollars += money;
		stringstream ss; // This could most likely be done better!
		ss << dollars; 
		return ss.str();
	}
	
	string withdraw(int money){
		dollars -= money;
		stringstream ss;
		ss << dollars;
		return ss.str();
	}
		
	string return_total(){
		stringstream ss;
		ss << dollars;
		return ss.str();
	}
	
	void set_total(int money){
		dollars = money;
	}
		
}; 


now for the function that does the file work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

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{
		const char * 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!
use ios flags

ios::app or ios::ate
Thank you for the reply!

Unfortunately, the addition of ios::app or ios::ate do not solve my problem. The file is still being reset, almost as if there is an invisible

 
ofile << "";


at the beginning of my do loop. This is not the case, as ios::app would fix that, but this is simply the best way I can describe the problem
Last edited on
when you run program for first time then there is nothing in file.And you are inputting from file first.So you get nothing.
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. ?
Last edited on
Topic archived. No new replies allowed.