Is it possible to store a new line and store at the above of the text file

I need to store the last 3 deposit that I have in my deposit.

I have few option but i dont which would be easy

1.Store all deposit to the text file (store always in a new line), and display the last three deposit from the text file.

dep 1 - 60
dep 2 - 40
dep 3 - 100 print 100
dep 4 - 50 print 50
dep 5 - 50 print 50


2. I think this option is more difficult, when it reach deposit 4, to get rid deposit 1

So when i make a deposit 4, the deposit 1 get's ride
dep 2 - 50
dep 3 - 100
dep 4 - 70


Right now i can only display one deposit (last one), then i close the program and run again and i make another deposit it overwrites a new deposit.

My code.
To show sure my deposit that has been made.

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
double depSave  ()
{
	int option;
	float dep1,dep2,dep3;									// Declare your variables
		dep1 = dep2 = dep3 = 0;							   // Set them all to 0
		
		system ("cls");
		string path = "deposit.txt";					   // Storing your filename in a string
		ifstream fin;									  // Declaring an input stream object

		fin.open(path);			                          // Open the file
		if(fin.is_open())			                     // If it opened successfully
		{
			fin >> dep1 >> dep2 >> dep3;				  // Read the values and
														 // store them in these variables
			fin.close();								// Close the file
		}

		cout<<"This are your last 3 deposit."<<endl;
		cout << dep1 << '\n';
		cout << dep2 << '\n';
		cout << dep3;

		
	


Deposit Function

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
double deposit(double balance)

{
	int option;
	double addSum = 0.00;
	double saveDeposit = 0;


					system("CLS");
					cout<< "Welcome to deposit."<<endl;
					cout<<"Enter a sum you wish to add to your account:";
			
		cin>> fixed>>setprecision(2)>>addSum;						// It means write to fil >> fixed command >> setprecision 	
																	// Means to make it number+2 places so like 3 parts total (part 1,part2,part3 like 550.26 (part 1 is 550, part 2 is 2 and part 3 is 6)
		
		fstream myfile;
		myfile.open ("Money.txt");												// Open file
			
		balance += addSum;														// Add assignment operator, means a=a+1 such an operation
		

		myfile << fixed<<setprecision(2)<<balance;								// Store to balance
		

		myfile.close();


		//save deposit

					
								myfile.open ("deposit.txt");												// Open file
			
								saveDeposit = addSum;														// Add assignment operator, means a=a+1 such an operation
		
								myfile << fixed<<setprecision(2)<<saveDeposit;								// Store to balance
								myfile.close();

				

								
				cout<<"\nYour new balance is:"<<endl;
				printf("%.2f \n", balance);									// Print balance	 


Where is says "save deposit" in comment
that where it saves to the deposit text file, that going to be output to the depSave function.
How can i at least add a new line to a text file

1
2
3
4
5
6
7
8
9
myfile.open ("deposit.txt");												
			
						saveDeposit = addSum;														
		
						myfile<<fixed<<setprecision(2)saveDeposit;								
								


                                             myfile.close();
Last edited on
First, You should store all your file's data into list/vector/etc of strings. Then, do your work with the data,and finally writes it into the file.
Topic archived. No new replies allowed.