I've been given an assignment as follows and I'm having trouble getting it to work properly, hoping if anyone out there can lend word on how to get it running?
"A bank account starts out with $10,000. Interest is compounded monthly at 6% per year (0.5% per month). Every month, $500 is withdrawn to meet college expenses. After how many years is the account depleted?
Suppose the numbers ($10,000, 6%, $500) are inputs to the program. Are there values for which the algorithm you developed would not terminate? If so, make sure you change your algorithm such that it always terminates. If the account will not be depleted you should output a message to the user saying that “Account will never be depleted!”.
The output from this program is the number of years the account is depleted. The program should continue processing different sets of input data until (0,0,0) is entered by the user."
Sample input:
10000 6 500
20000 7 300
15000 5 700
9000 3 1000
5000 2 300
8000 12 70
0 0 0
Source code:
#include<isotream>
#include<conio.h>
#include<iomanip>
#include<fstream>
using namespace std;
#define in_file "data.txt"
#define out_file "result.txt"
int main()
{
ifstream ins;
ofstream outs;
ins.open(in_file);
outs.open(out_file);
double start;
double interest;
double withdraw;
double balance;
int month;
do
{
ins >> start;
ins >> interest;
ins >> withdraw;
interest=interest/100/12;
month=0;
balance=start;
if ((balance * interest - withdraw >= 0) && (start!=0))
outs << "Account will never be depleted!" << endl;
else
while (balance + balance * interest - withdraw >= 0)
{
balance = balance + interest * balance;
balance = balance - withdraw;
month = month + 1;
}
if (month%12 > 0)
{
outs << setprecision(0) << fixed;
outs << month/12 << " year(s) and " << month%12 << " month(s). " << endl;
}
}while (start!=0);
ins.close();
outs.close();
return 0;
}
The main problem I think I'm having is with the 20000 7 30 input data.
Any help would be great!