0

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.

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.

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();
                }
Ken White
  • 123,280
  • 14
  • 225
  • 444

1 Answers1

0

Instead of myfile.open ("balance.txt"); use myfile.open ("balance.txt", ios::app); That will make it to where you will append to the file. In regards to only keeping the last three lines, that will required you reading in the file, and parsing the text, and only keeping what you want, then outputting it.

http://www.cplusplus.com/reference/fstream/basic_ofstream/open/

David
  • 4,744
  • 5
  • 33
  • 64