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!

Recommended Answers

All 3 Replies

The monthly interest will be bigger than the withdraw amount in that case in which you have 20000 7 30 as input.Tried it myself :)

#include <iostream>

int main ()  {
   float Amount=20000;
   float Interest=7;
   float Withdrawl=30;

   int Months=0;
   float MonthlyInterest=(Amount*(Interest/100))/12;
   float MonthlyTotal=Amount+MonthlyInterest;   

   if (MonthlyInterest<Withdrawl)   {
      while ((MonthlyTotal-=Withdrawl)>0)  {
         Months++;
       }
    }
   int Years=(Months-Months%12)/12;
   std::cout << "Years:" << Years << "  Months:" << Months%12;
 }
commented: Still not doing the right thing -1

are you in culham's class...?

first thing i can see wrong is you used isotream instead of iostream in the header.

Also, keep it as simple as possible- when you define the input and output files, you can skip doing that before the main, and use the following in main():

ifstream ins;
ofstream outs;
ins.open("data.txt");
outs.open("result.txt");

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.