File problem!

Hi! I need help with saving data into a text file. What I did won't work and I'm not sure what I'm doing wrong. I just need the results to save into a separate file that I have created in the project folder, after all of the calculations.

Also the loop must break if the balance is zero in any iteration and I don't think my program does that..

Can someone please help me with these two problems?

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
76
77
78
79
80
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
    double annualInterest,   startingBal,
           accountBalance,   withdrawn,
           deposit;
    double totalDeposit = 0, totalInterestEarned = 0,
           monthlyInterestEarned, totalWithdrawn = 0;
    int    months, i;

    ofstream outFile;
    outFile.open("accountresults");

    // Prompting the user to enter information
    cout << "Enter the annual interest rate: " << endl;
    cin >> annualInterest;

    cout << "Enter the starting balance: " << endl;
    cin >> startingBal;
    accountBalance = startingBal;

    cout << "Enter the number of months that have passed\n";
    cout << "since the account was established: " << endl;
    cin >> months;

    // Running each month and gathering data
    for (i = 1; i <= months; i++)
    {
        cout << "Enter the following information for month " << i << ": " << endl;

        do // Asking for the monthly deposit
        {
            cout << "How much money was deposited? ";
            cin >> deposit;
            if (deposit < 0)
            cout << "It should be a positive number.\n\n";

        }   while (deposit < 0);
            accountBalance += deposit;

        do // Asking for the monthly withdrawals
        {
            cout << "How much money was withdrawn from account? ";
            cin >> withdrawn;
            if (withdrawn < 0)
            cout << "It should be a positive number.\n\n";

        }   while (withdrawn < 0);
        accountBalance -= withdrawn;

        monthlyInterestEarned = accountBalance * (annualInterest / 12);
        accountBalance += monthlyInterestEarned;

        totalDeposit += deposit;
        totalWithdrawn += withdrawn;
        totalInterestEarned += monthlyInterestEarned;

    }

    // Displaying the results
    outFile << fixed << setprecision (2);
    outFile << "Ending balance: " << accountBalance << endl;
    outFile << "Total deposit: " << totalDeposit << endl;
    outFile << "Total withdrawn: " << totalWithdrawn << endl;
    outFile << "Total interest earned: " << totalInterestEarned << endl;

    outFile.close();

    return 0;
}



//If the balance is less than zero in any iteration, you must break your loop and send the information to the file along with a message.
It works for me, the balance isn't right but it outputs to a file.

check your math
 
        monthlyInterestEarned = accountBalance * (annualInterest / 12);
Last edited on
Topic archived. No new replies allowed.