Here is the question first of all: (I'm using visual c++)
Write a program that calculates the balance of a savings account at the end of a threemonth
period. It should ask the user for the starting balance and the annual interest rate. A
loop should then iterate once for every month in the period, performing the following:
A) Ask the user for the total amount deposited into the account during that month.
Do not accept negative numbers. This amount should be added to the balance.
B) Ask the user for the total amount withdrawn from the account during that
month. Do not accept negative numbers or numbers greater than the balance
after the deposits for the month have been added in.
C) Calculate the interest for that month. The monthly interest rate is the annual
interest rate divided by 12. Multiply the monthly interest rate by the average of
that month’s starting and ending balance to get the interest amount for the
month. This amount should be added to the balance.
After the last iteration, the program should display a final report that includes the following
information:
• starting balance at the beginning of the three-month period.
• total deposits made during the three months
• total withdrawals made during the three months
• total interest posted to the account during the three months
• final balance.
What I need is to solve that particular problem using a class. Any help is appreciated.
here is the code solved for the problem not using a class:
cout<<"Enter the annual interest rate: ";
cin>>annulInterestRate;
monthlyInterestRate=annulInterestRate/12.0;
cout<<"Enter the starting balance: ";
cin>>balance;
{
//A:Asking for deposit
cout<<"\nEnter the amount deposited into the account during the three months: ";
cin>>deposit;
while(deposit<0)
{
cout<<"\nEnter a valid amount deposited into the account during the three months: ";
cin>>deposit;
}
totalDeposit+=deposit;
balance+=deposit;
//B:Asking for withdraws
cout<<"\nEnter the amount of withdraws during the month: ";
cin>>withdraws;
while(withdraws<0)
{
cout<<"\nEnter a valid amount of withdraws during the month: ";
cin>>withdraws;
}
balance-=withdraws;
totalWithdraws+=withdraws;
while(balance<0)
{
cout<<"\nThe account has been closed.";
break;
}
}
//C:Calculateing the monthlyinterest
interestEarned=balance*monthlyInterestRate;
balance+=interestEarned;
totalInterestEarned+=interestEarned;
Then you need to learn about c++ classes and somehow define SavingsAccount class. This class could contain members for current balance, array of deposit values, array of withdrawal values. You can also add member functions like withdraw(), deposit(), etc.
Ok. So I would probably put current balance, deposit values, and withdrawals in the public function and the member functions in the private function? or do I have that backward. I'll check myself but any more advice is helpful.
int main ()
{
Account balance1;
balance1.setwithdraw(0);
balance1.setdeposit(0);
balance1.setInterest(42);
balance1.getwithdraw();
balance1.getdeposit();
balance1.computeInterest();
cout<<"Enter the amount withdrawn in three months: " <<balance1.getwithdraw()<<endl;
cout<<"Enter the amount deposited in three months: " <<balance1.getdeposit()<<endl;
int main ()
{
Account balance1;
balance1.setbal(0);
balance1.setwithdraw(0);
balance1.setdeposit(0);
balance1.setInterest(0);
balance1.getbal();
balance1.getwithdraw();
balance1.getdeposit();
balance1.computeInterest();
int choice;
double Bal, dep, with, intrate;
cout<<"Enter the monthly interest rate: "<<balance1.computeInterest()<<endl;
cin>>intrate;
cout<<"Enter the initial balance: "<<balance1.getbal()<<endl;
cin>>Bal;
cout<<"Enter the amount withdrawn in three months: " <<balance1.getwithdraw()<<endl;
cin>>with;
cout<<"Enter the amount deposited in three months: " <<balance1.getdeposit()<<endl;
cin>>dep;
int main ()
{
Account balance1;
balance1.setbal(0);
balance1.setwithdraw(0);
balance1.setdeposit(0);
balance1.setInterest(0);
balance1.getbal();
balance1.getwithdraw();
balance1.getdeposit();
balance1.computeInterest();
int choice;
double Bal, dep, with, intrate;
cout<<"Enter the monthly interest rate: "<<balance1.computeInterest()<<endl;
cin>>intrate;
cout<<"Enter the initial balance: "<<balance1.getbal()<<endl;
cin>>Bal;
cout<<"Enter the amount withdrawn in three months: "<<balance1.getwithdraw()<<endl;
cin>>with;
cout<<"Enter the amount deposited in three months: "<<balance1.getdeposit()<<endl;
cin>>dep;
system ("pause");
return 0;
}
You wonder why there is zeros printed? It is because you printed them. See in bold at lines 16,18,20,22.
Make it a habit that when you report that you have problem, paste the latest code and also the error details for us to see what's causing it. It's hard to imagine you know...