I need a little bit of help figuring out this problem

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:

#include<iostream>
using namespace std;


int main()
{
double annulInterestRate,
balance,
monthlyInterestRate,
deposit,
totalDeposit=0,
withdraws,
totalWithdraws=0,
interestEarned,
totalInterestEarned=0;

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;

cout<<"\nEnding balance: " <<balance;
cout<<"\nTotal amount of deposit: " <<totalDeposit;
cout<<"\nTotal amount of withdraws: " <<totalWithdraws;
cout<<"\nTotal interest earned: " <<totalInterestEarned<<endl;

system("pause");
return 0;
}
Last edited on
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.
I don't understand what you are saying. Better study first about classes so you can understand what I am saying.
this is what I've got so far, it works but it's not necessarilly what the problem is asking for...

#include<iostream>
using namespace std;

class Account
{
private:
int Balance;
int monthInterestrate;
double deposit;
double withdraw;
double totalB;

public:

Account();
double totalBalance ()
{return totalB = Balance + (deposit+withdraw) ;}

double getwithdraw()

{return withdraw;}

void setwithdraw(double w)
{withdraw = w;}
double getdeposit()


{return deposit;}

void setdeposit(double d)
{deposit = d;}

double computeInterest()

{return Balance*monthInterestrate/12; }

void setInterest(double i)
{monthInterestrate = i;}
};

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;

system ("pause");
return 0;
}

Account::Account()
{ totalB = 0 ;
withdraw = 0;
deposit = 0;
monthInterestrate = 0;}
So I got it to let me enter the values myself. Still got problems with zero's appearing and now I need help with the calculations part.

#include<iostream>
using namespace std;

class Account
{
private:
int Balance;
int monthInterestrate;
double bal;
double deposit;
double withdraw;
double totalB;

public:

Account();
double totalBalance ()
{return totalB = Balance + (deposit+withdraw) ;}

double getbal()
{return bal;}
void setbal(double b)
{bal = b;}

double getwithdraw()

{return withdraw;}

void setwithdraw(double w)

{withdraw = w;}

double getdeposit()

{return deposit;}

void setdeposit(double d)
{deposit = d;}

double computeInterest()

{return Balance*monthInterestrate/12; }

void setInterest(double i)
{monthInterestrate = i;}
};

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;
}

Account::Account()
{ totalB ;
bal ;
withdraw ;
deposit ;
monthInterestrate ;}
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
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.
Yea, but when I remove the zero's I get errors. So I'm not sure how to fix that. I've been trying around and it's not working out.

Please excuse my hard time grasping the subject but I'm pretty much teaching this to myself and it's a little frustrating.

Anyways, how do I get the code not to make an error after removing the zero's? How do I get the calculations to work?
Last edited on
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...
Topic archived. No new replies allowed.