Need help to write calculations onto a file

So Im writing a program that calculates bank deposits, interests, and withdraws. I need to output my final calculations to a file called accountreport.txt but i dont know how any help would be appreciated

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  #include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    double balance,
        annual_interest_rate,
        monthly_rate,
        time_period,
        deposited,
        withdrawn,
        total_deposits,
        total_withdrawls,
        total_interest,
        month=1,
        monthly_intrest_amount,
        final_balance;
    
    cout <<"What is the starting balance"<<endl;
    cin >> balance;
   
    cout <<"What is the annual interest rate"<<endl;
    cin >> annual_interest_rate;
    
    cout <<"how many months in your time period"<<endl;
    cin >> time_period;
    
 
    
    
    while(time_period>0)
    {
     
        cout <<"What was the total deposited in month "<<month<<endl;
        cin >> deposited;
        if(deposited<0)
        {
            cout <<"You cannot deposit a negative amount ending program"<<endl;
             return 0;
        }
        balance+=deposited;
        total_deposits+=deposited;
        cout <<"What was the total withdrawn in month "<<month<<endl;
        cin >> withdrawn;
        if(withdrawn<0)
        {
            cout <<"You cannot withdraw a negative amount ending program"<<endl;
             return 0;
        }
        if(withdrawn>balance)
        {
            cout <<"You cannot withdraw more than whats in your balance"<<endl;
            cout <<"Terminating Program"<<endl;
             return 0;
        }
        balance-=withdrawn;
        total_withdrawls+=withdrawn;
        monthly_rate=(annual_interest_rate / 12)/100.0;
        monthly_intrest_amount=monthly_rate * balance;
        total_interest+=monthly_intrest_amount;
        balance=(balance + monthly_intrest_amount);
        time_period--;
        month++;
       
    }
   cout<<"The total amount deposited was $"<<total_deposits<<endl;
   cout<<"The total amount withdrawn was $"<<total_withdrawls<<endl;
   cout<<"The total amount of interest posted was $"<<monthly_intrest_amount<<endl;
   cout<<"The final balance is $"<<balance<<endl;
    
    return 0;
}



The last four couts are what i need to get written on a file.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <fstream>
#include <iostream>

using namespace std;

int main(){

ofstream output("data.txt");

output << "HI MOM!";

output.close();

return 0;

}


Last edited on
Topic archived. No new replies allowed.