I have this question. could anyone help me how to write C++ code for it:
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.

here is a sample of input in file:
10000 6 500
20000 7 300
15000 5 700
9000 3 1000
5000 2 300
8000 12 70
0 0 0

This is what I've gotten so far, but it has many mistakes and i cannot fix it. please help asap and thanks in advance:

#include <iostream>; // requires for keyboard and screen I/O

#include <fstream>; // requires for external file streams
#include <cmath>; // used for mathematical operations
// Associating program identifiers with external file names
#define in_file "data.txt" 
#define out_file "result.txt" 

using namespace std;
void main()
{
	
ifstream ins; // associates ins as an input stream 
ofstream outs; // associates outs as an output stream

// local values
float
originalAmount, monthlyInterest, yearlyInterest, balance, Expenses;
int Month, Year, Total, Time;

 ins.open(in_file); 
 outs.open(out_file);

 monthlyInterest = (yearlyInterest/12);
ins >> originalAmount;
while (originalAmount != -1) // read a number from file
{
// Calculate monthly payment.
	  
	Total = (monthlyInterest * originalAmount)- Expenses;
	balance = (monthlyInterest * originalAmount);
	Year = Month / 12;
	Month = Month % 12;
	Time = Year + Month;

cout << "The account will be depleted in" << Year << "year(s)and" << Month << "month(s)" << endl;

outs << "The account will be depleted in" << Year << "year(s)and" << Month << "month(s)" << endl;
ins >> originalAmount;
}
	
    cout << "The originalAmount balance" << originalAmount << endl;
  
    cout << "The yearly interest rate: " << yearlyInterest << endl;
   
	cout << "Withdrawal expenses per month:" << Expenses << endl;
      
// press any key to exit
system("pause");

// end while loop
	ins.close();
	outs.close();

}// end program

What are the specific issues that you are having with it? "It has many mistakes" doesn't give anyone much to go on.

I am beginner in the world of C++, so I am not quit sure about the code
I do not know how to fix it
it has to show how long the account will be depleted
i am totally lost with "loops" and "while"
how am I suppose to answer this
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.

could you provide some help

I continued working on the code and this is what I got so far:

#include <iostream>; // requires for keyboard and screen I/O
#include <conio.h>; // for getch 
#include <fstream>; // requires for external file streams

// Associating program identifiers with external file names
#define in_file "data.txt" 
#define out_file "result.txt" 

using namespace std;
int main()
{

ifstream ins; // associates ins as an input stream 
ofstream outs; // associates outs as an output stream

// local values
float
originalAmount, yearlyInterest, Expenses, final; final = 0;
float 
monthlyInterest,Total, balance;
int Month, Year, Time;

 ins.open(in_file); 
 outs.open(out_file);
ins >> originalAmount;
ins >> yearlyInterest;
ins >> Expenses;
ins >> Month;
ins >> Year;
outs << fixed;

while (originalAmount >= 0) // read a number from file
{
// Calculate monthly payment.
     monthlyInterest = (yearlyInterest/12);  
    balance = (monthlyInterest * originalAmount);
    Total = balance - Expenses;
    Year = Month / 12;
    Month = Month % 12;
    Time = Year + Month;

cout << "The account will be depleted in " << Year << " year(s)and " << Month << " month(s)" << endl;

outs << "The account will be depleted in " << Year << " year(s)and " << Month << " month(s)" << endl;

ins >> originalAmount;
}// end while loop
cout << "The originalAmount balance" << originalAmount << endl;
cout << "The yearly interest rate: " << yearlyInterest << endl;
cout << "Withdrawal expenses per month:" << Expenses << endl;




// press any key to exit
system("pause");


    ins.close();
    outs.close();

    getch ();
return 0;
}// end program

it is a bit different from the previous one, but

Month has no value in your program, so you end up with whatever junk is in there when it was declared. You declare it, and then you try to use it in calculations but nowhere is it assigned. Your program should be finding the value of month to deplete the account. You'll want to use a loop and have a counter to determine how many times you go through the loop. Write it out in pencil and paper first to clarify the calculations.

Hint, to test if it will never be depleted, see if the interest earned for the month minus the college expenses is positive or negative.

I've gotten an infinite loop

Okay, well make sure the condition you are testing for changes somewhere in the loop, otherwise it will loop forever. Post the modified code.

I did not get what you mean but here is the modified one, and thanks in advance

#include <iostream>; // requires for keyboard and screen I/O
#include <conio.h>; // for getch 
#include <fstream>; // requires for external file streams

// Associating program identifiers with external file names
#define in_file "data.txt" 
#define out_file "result.txt" 

using namespace std;
int main()
{

ifstream ins; // associates ins as an input stream 
ofstream outs; // associates outs as an output stream

// local values
float
originalAmount, yearlyInterest, Expenses;
float 
monthlyInterest,Total, balance;
int Month, Year, Time;

 ins.open(in_file); 
 outs.open(out_file);
ins >> originalAmount;
ins >> yearlyInterest;
ins >> Expenses;
ins >> Month;
ins >> Year;
outs << fixed;

while (originalAmount >= 0) // read a number from file
{
// Calculate monthly payment.
     monthlyInterest = (yearlyInterest/12);  
    balance = (monthlyInterest * originalAmount);
    Total = balance - Expenses;
    Year = Month / 12;
    Month = Month % 12;
    Time = Year + Month;
    Month = Month + 1;
cout << "The account will be depleted in " << Year << " year(s)and " << Month << " month(s)" << endl;

outs << "The account will be depleted in " << Year << " year(s)and " << Month << " month(s)" << endl;

ins >> originalAmount;
}// end while loop
cout << "The originalAmount balance" << originalAmount << endl;
cout << "The yearly interest rate: " << yearlyInterest << endl;
cout << "Withdrawal expenses per month:" << Expenses << endl;





// press any key to exit
system("pause");


    ins.close();
    outs.close();

    getch ();
return 0;
}// end program

I was saying that if you weren't modifying originalAmount in the loop then the while loop condition would never be met. You are reading it in at the end, so that's fine. However, even when the last datapoint is read in, won't originalAmount still be >=0 causing the loop to continue? Change the condition to require it to be greater than 0 only.

I would have a second while loop inside of the first one that counts down and increments month. Then you can output month, reset it to zero and go through the loop again for the second value. Check the order of your calculations, as you need to wait until you know the final months value before you find the number of years and spare months.

Also, please use code tags when posting your code. Type [code] //code goes here [/code]. A mod was nice enough to put them in your first post.

could you show me an example of how to use a second while "that counts down and increments month" please

double runningTotal;  //set equal to first dollar amount
int month=0;
while(originalAmount > 0)
{
  //check to see if we can in fact count down to $0.00
  //if so, continue on, if not, tell the user and skip down to read in another val
   while(runningTotal is more than a cent)  //might be hard to hit zero so go for a penny
   {
       //adjust running total by months interest and subtract expenses


      month++;
    }
    //set runningtotal to next amount
}

it is getting confusing to me
could you please show me how to solve the program

Nope. I will not. It's against site policy, and you will definitely not learn anything that way.

Keep at it, you're almost there, but this has to be your effort.

this is getting confusing to me ,
so could you please show me how to solve this program and I appreciate your assistance

alright
but could you please explain it to me in a very simple way because i feel i do not understand what you say

I think your classmate also posted a similar question since i can see it is the same problem.Check his thread too for answers.Good luck

so if the total amount is greater than the original one, the account will last forever?

when is the total amount will be depleted at a specific time?

Check his thread too for answers.Good luck

Or you could let people write their own code.

Learn the rules. Thank you.

so if the total amount is greater than the original one, the account will last forever?

If I give you 10,000 and the amount I'm adding to that $10,000 is positive (so the interest earned exceeds the tuition paid out), how could the account go negative or to zero?

when is the total amount will be depleted at a specific time?

As we're computing the interest, adding it in, and subtracting the tuition, we'll have 10,000, then 9,900 the next month (I'm making up numbers, but you get the idea), then 9,800 the next month after that, eventually you'd find that after 100 months you'd be down to zero.

does using only while loop work or do I have to use other ones like if and else

The while loop will work on its own. You'll test a condition like amount > 0 with it, just like you would in an if statement, but you don't actually need one.

could you please tell me what do you mean by this
" //check to see if we can in fact count down to $0.00
//if so, continue on, if not, tell the user and skip down to read in another val"

Check and see if the account can be depleted (so is the amount we're adding every month, interest - tuition, is less than zero).

If it can, then go ahead and see how many months it will take using the inner while loop.

If it can't be depleted, skip over the while loop and get the next value ins >> originalAmount; .

For checking if it can be depleted, you would need an if statement, so sorry if my prior comment was misleading (I thought you were asking if you needed an if statement to test the condition of the while loop).

This is a modified one

#include <iostream>; // requires for keyboard and screen I/O
#include <conio.h>; // for getch 
#include <fstream>; // requires for external file streams

// Associating program identifiers with external file names
#define in_file "data.txt" 
#define out_file "result.txt" 

using namespace std;
int main()
{
	
ifstream ins; // associates ins as an input stream 
ofstream outs; // associates outs as an output stream

// local values
double
originalAmount, yearlyInterest, Expenses;
double
Total;
int Month = 0;

 ins.open(in_file); 
 outs.open(out_file);
ins >> originalAmount;
ins >> yearlyInterest;
ins >> Expenses;
ins >> Month;

outs << fixed;


while (originalAmount > 0) 
{
		ins >> Total;
	
		while (Total > 0) 
			{
		Total = ((yearlyInterest/12)*originalAmount)-Expenses;
	    cout << "Account will never be depleted." << endl; 
			Month ++;
	    
			}
	    int Month/12; 
	    Month = Month%12; 

	   cout << "The account will be depleted in " << Month/12 << " year(s) and " << Month << " Month(s)."; 

	   cout << "Final balance will be: " << Total << " dollars."; 
	
		
}// end while loop


// press any key to exit
system("pause");


	ins.close();
	outs.close();
	
	getch ();
return 0;
}// end program

there is ';' missing before '/'
I checked it , but I do not know where is it

Line 28 and line 35, there's no such value in the file.

Determine what Total is, but don't loop over it. Then, if total is less than zero, loop until the original amount is less than or equal to zero, adding the total to the original amount each month.

Line 44 is coming out of nowhere, you already have Month as a variable (that shouldn't even compile).

Okay, you're getting closer. Take some time with it and check your answers with pencil and paper to make sure your program is doing the right thing.

Hint: you'll need to take in your next line of the data file somewhere within the outer while loop.

if ((originalAmount * (yearlyInterest/12)) - Expenses > 0)
{
        outs << "Account will never be depleted!" << endl;
        ins >> originalAmount;


        while (.....) 
            {
                ins >> ....;

        cout << "Account will be depleted." << endl; 
            Month ++;

            }

This is where I am now
could you please give some hints on how to fill the blanks

Our posts crossed. Don't redeclare the variable there anyway, but declaring something /2 is not the proper syntax.

I have given you hint after hint. I'm not going to do this for you. Take more than 2 minutes to fix it and repost what you have.

this is what i got but the sentence 'account will never be depleted' is repeating

#include <iostream>; // requires for keyboard and screen I/O
#include <conio.h>; // for getch 
#include <fstream>; // requires for external file streams

// Associating program identifiers with external file names
#define in_file "data.txt" 
#define out_file "result.txt" 

using namespace std;
int main()
{
	
ifstream ins; // associates ins as an input stream 
ofstream outs; // associates outs as an output stream

// local values
double
originalAmount, yearlyInterest, Expenses;
double
Total = 0;
int Month = 0;

 ins.open(in_file); 
 outs.open(out_file);
ins >> originalAmount;
ins >> yearlyInterest;
ins >> Expenses;


outs << fixed;


if ((originalAmount * (yearlyInterest/12)) - Expenses > 0)
{
        outs << "Account will never be depleted!" << endl;
		ins >> originalAmount;
	
	
		while (Total + (originalAmount * (yearlyInterest/12)) - Expenses > 0)
			{
            Total = Total + ((yearlyInterest/12) * originalAmount);
            Total = Total - Expenses;
            Month = Month + 1;
 
				
		Total = ((yearlyInterest/12)*originalAmount)-Expenses;
	    cout << "Account will be depleted." << endl; 
			Month ++;
	    
			}
	    
	    Month = Month%12; 

	   cout << "The account will be depleted in " << Month/12 << " year(s) and " << Month << " Month(s)."; 

	   cout << "Final balance will be: " << Total << " dollars."; 
	
		
}// end while loop


// press any key to exit
system("pause");


	ins.close();
	outs.close();
	
	getch ();
return 0;
}// end program

Your original specification says interest will be compounded monthly, so line 39 is incorrect. Look it up online if you're not sure what "compounded monthly" means.

You've lost your outer while loop somewhere.

It doesn't ask for you to put a message that the account will be depleted, just a message at the end telling the user how many months it will take.

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.