Open and store a value to the text file

This my deposit function, i want to open the file (balance), then add the sum and store in on the text file.

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
float deposit( int x, int currentBalance)
		{
			cout<<"\n"<<endl;
			cout<<"Welcome to the deposit area"<<endl;
			
			cout << "Your new balance is:" << balance <<endl;

			cout<<"Enter a sum you wish to add to your account:";
			
			cin>> x ;

			balance = x + currentBalance;

			cout << "Your new balance is:" << balance <<endl;

			 fstream myfile;
			  myfile.open ("balance.txt");
			  myfile << balance;
			  myfile.close();


			cout<<"\n"<<endl;
			cout<<"Press 0 for further action or press 9 to exit." <<endl;
			
			cin >> option;

			if (option == 9)
			
			{
			exit(0);
And? What's the problem you're having?
the problem is when i start the program it starts on 0, instead starting the value that i have input from the last deposit i have made.
So, when the program starts, you need to read the file, and initialise the balance with the value read from the file.
yes, sir. That is correct.
OK... so what's the problem you're having with doing that?
the problem i cant replace the value to the balance.

I dont know what to do, or look for.
What do you mean? Replace the value where? In the file, when you write the new balance to the file? Or in memory when running the program.

If you want help, you're going to have to be clear and detailed about what it is you're looking for. We're not interested in playing Twenty Questions with you.
Sir, the code i got there is store the value to text file called (balance.txt) and it sums up, every time i add more deposit.

but when i close the program the balance goes back to 0. I want to store the balance permanent , so every time i run the balance is there and i can add another deposit.

English is not first language, find quite difficult to explain what i want.


thank you.
OK. But we're not going to write your program for you. You'll need to have a go at writing it yourself. Then, if you have a specific problem that you can't solve, we can help solve it.
it's been days, about 8 hours a day, nights without sleeping. Till can't find nothing.
Are you serious? You can find out information on how to write to a file in C++, but you can't find anything at all on how to read from a file?

There's a tutorial about it right here on this site, FFS!
I don't understand why you've made another thread for this problem.
How are you setting the value for currentBalance?
Topic archived. No new replies allowed.