Store a value to myfile, to a new line of the text file

This is my deposit, i want to save the value that is on (currentBalance) to the text file call balance. Right now I only know to store a sentence "Writing this to a file1". I want to write to a new line, not rewrite.

But I want to store only last 3 deposit, because later on, I want to let the use to see the last 3 deposit he did.

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
float deposit( int y, int x)
        {
            cout<<"\n"<<endl;
            cout<<"Welcome to the deposit area"<<endl;
            cout<<"Enter a sum you wish to add to your account:";
            cin>> y ;

            currentBalance = y + x ;

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

             ofstream myfile;
              myfile.open ("balance.txt");
              myfile << "Writing this to a file1.\n";
              myfile.close();


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

            cin >> option;

            if (option == 9)

            {
            exit(0);
            }

            else if (option == 0 )
            {
            return 1;
            }

            else
            {
            exit(0);
            }
        }  



How do I store what is on current balance.

This is my balance code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
float balance(int x)
        {
            cout<<"Welcome to the balance area"<<endl;
            cout<< "Your balance is: " <<(char)156 <<endl;      

                string line;
                ifstream myfile ("balance.txt");
                if (myfile.is_open())
                {
                    while ( getline (myfile,line) )             
                {
                        cout << line << '\n';
                    }
                    myfile.close();
                }
You do so with ofstream.

Same syntax of cin.

1
2
3
4
5
6
7
                ofstream myfile ("balance.txt");
                if (myfile.is_open())
                {
                    myfile >> balance;

                    myfile.close();
                }
Last edited on
Your code comes with 11 error.

I dont know if i wrote it wrong, but i want to store a new value that is on (currentBalanace).

I made a deposit (currentBalance = x + y)

And change the new currentBalance to the text file in a new line.

later on, i want to output the last 3 deposit that the user did, which has been store on the text file.
Whoops I made a mistake in what I was trying to say. I meant:

1
2
3
4
5
6
7
                ofstream myfile ("balance.txt");
                if (myfile.is_open())
                {
                    myfile << balance;

                    myfile.close();
                }


(This code isn't just compile n' run btw. If you want a function you can use here's one:
1
2
3
4
5
6
7
8
9
10
bool SaveBalance(float balance)
{
    ofstream file("balance.txt");

    if(!file) return false;

    file << balance;

    return true;
}
Last edited on
Avilius how do i store.

what is in currentBalance to a text file.

1
2
3
4
5
6
7
8
 currentBalance = y + x ;

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

             ofstream myfile;
              myfile.open ("balance.txt");
              myfile << "Writing this to a file1.\n";
              myfile.close();


so it will be myfile << then what?
Wait, do you want to write a string to the file or the balance to the file?
I want to store the new balance, because a deposit has been made to the currentBalance.
I found the way to store the new balance.

What was in:

myfile << "Writing this to a file1.\n";

To

myfile << currentBalance;
Topic archived. No new replies allowed.