Hello, new here, I am having trouble with a part of my C++ assignment I am doing for university.

The assignment is about writing a structure for a bank account and then subsequently enter values for the account's number, balance, interest rate, term of holding and deposit/withdrawal amounts. The part I am having trouble with involves displaying the balance for each month, and involves a while or for loop (I have tried both, with no progress).

Here is my code, which works aside from the section I am having trouble with which is highlighted in red.:

//Allows a user to enter an account number and balance for a bank account.
#include <iostream>
using namespace std;
struct BankAccount
{
	int accountNumber;
	double accountBalance;
	double interestRate;
};
int main ()
{
	BankAccount banker1;
	BankAccount banker2;
	do
	{
		cout<<"Please enter the account number for the first account: "<<endl;
		cin>>banker1.accountNumber;
		if(banker1.accountNumber < 1000 || banker1.accountNumber > 9999)
		{
			cout<<"That is not a vaild account number, please enter a number between 1000 and 9999."<<endl;
		}
	}
	while(banker1.accountNumber < 1000 || banker1.accountNumber > 9999);

	do
	{
		cout<<"Please enter the account balance for the first account: "<<endl;
		cin>>banker1.accountBalance;
		if(banker1.accountBalance < 0 || banker1.accountBalance > 100000)
		{
			cout<<"That is not a vaild account balance, please run the program again and enter a number greater than 0."<<endl;
		}
	}
	while(banker1.accountBalance < 0 || banker1.accountBalance > 100000);

	do
	{
		cout<<"Please enter the interest rate for the first account: "<<endl;
		cin>>banker1.interestRate;
		if(banker1.interestRate < 0 || banker1.interestRate > 0.15)
		{
			cout<<"That is not a vaild interest rate, please enter a number between 0 and 0.15."<<endl;
		}
	}
	while(banker1.interestRate < 0 || banker1.interestRate > 0.15);

	do
	{
		cout<<"Please enter the account number for the second account: "<<endl;
		cin>>banker2.accountNumber;
		if(banker2.accountNumber < 1000 || banker2.accountNumber > 9999)
		{
			cout<<"That is not a vaild account number, please run the program again and enter a number between 1000 and 9999."<<endl;
		}
		do
		{
			if(banker1.accountNumber == banker2.accountNumber)
			{
				cout<<"That is not a vaild account number, please run the program again and enter a different account number between 1000 and 9999."<<endl;
				cin>>banker2.accountNumber;
			}
		}
		while(banker1.accountNumber == banker2.accountNumber);
	}
	while(banker2.accountNumber < 1000 || banker2.accountNumber > 9999);

	do
	{
		cout<<"Please enter the account balance for the second account: "<<endl;
		cin>>banker2.accountBalance;
		if(banker2.accountBalance < 0 || banker2.accountBalance > 100000)
		{
			cout<<"That is not a vaild account balance, please run the program again and enter a number greater than 0."<<endl;
		}
	}
	while(banker2.accountBalance < 0 || banker2.accountBalance > 100000);

	do
	{
		cout<<"Please enter the interest rate for the second account: "<<endl;
		cin>>banker2.interestRate;
		if(banker2.interestRate < 0 || banker2.interestRate > 0.15)
		{
			cout<<"That is not a vaild interest rate, please enter a number between 0 and 0.15."<<endl;
		}
	}
	while(banker2.interestRate < 0 || banker2.interestRate > 0.15);

	int checkDigit1 = banker1.accountNumber % 5;
	int checkDigit2 = banker2.accountNumber % 5;

	int numberOfYears;
	do
	{
		cout<<"Please enter the number of years for the term the accounts will be held: "<<endl;
		cin>>numberOfYears;
		if(numberOfYears < 1 || numberOfYears > 10)
		{
			cout<<"That is not a vaild term duration, please enter a number between 1 and 10."<<endl;
		}
	}
	while(numberOfYears < 1 || numberOfYears > 10);

	double monthlyDepositAmount;
	do
	{
		cout<<"Enter an amount to be deposited into both accounts each month: "<<endl;
		cin>>monthlyDepositAmount;
		if(monthlyDepositAmount < 0)
		{
			cout<<"That is not a valid deposit amount, please enter a number greater than 0."<<endl;
		}
	}
	while(monthlyDepositAmount < 0);

	double monthlyWithdrawalAmount;
	do
	{
		cout<<"Enter an amount to be withdrawn from both accounts each month: "<<endl;
		cin>>monthlyWithdrawalAmount;
		if(monthlyWithdrawalAmount < 0)
		{
			cout<<"That is not a valid withdrawal amount, please enter a number greater than 0."<<endl;
		}
	}
	while(monthlyWithdrawalAmount < 0);

	int year;
	int month;
	double monthlyInterestRate1 = banker1.interestRate / 12;
	double monthlyInterestRate2 = banker2.interestRate / 12;
	double increment1;
	double increment2 = monthlyInterestRate2;
	
	

	cout<<"Starting balance for Account 1: "<<banker1.accountBalance<<endl;
	cout<<"Starting balance for Account 2: "<<banker2.accountBalance<<endl;

	for	(year = 1; year <= numberOfYears; year ++)
	{
		cout<<"Year "<<year<<"	"<<endl;
		for(month = 1; month <= 12; month ++)
		{
			cout<<"Month "<<month<<"	"<<endl;
			for(increment1 = monthlyInterestRate1; increment1 <= banker1.interestRate; increment1 ++)
			{
				double newMonthlyBalance1 = banker1.accountBalance;
				newMonthlyBalance1 = (newMonthlyBalance1 * (1 * monthlyInterestRate1)) + monthlyDepositAmount - monthlyWithdrawalAmount;
				cout<<"Balance for first account: "<<newMonthlyBalance1<<endl;
			}
			for(increment2 = monthlyInterestRate2; increment2 <= banker2.interestRate; increment2 ++)
			{
				double newMonthlyBalance2 = (banker2.accountBalance * (1 + monthlyInterestRate2)) + monthlyDepositAmount - monthlyWithdrawalAmount;
				cout<<"Balance for second account: "<<newMonthlyBalance2<<endl;
			}
		}
		cout<<endl;
	}
};

Any help is welcome, and thank you for reading.

Recommended Answers

All 2 Replies

You have lots of unused variables and in here you had 2 for-loops that were not needed.

cout<<"Starting balance for Account 1: "<<banker1.accountBalance<<endl;
cout<<"Starting balance for Account 2: "<<banker2.accountBalance<<endl;

double newMonthlyBalance1 = banker1.accountBalance;
double newMonthlyBalance2 = banker2.accountBalance;

for(year = 1; year <= numberOfYears; year ++)
{
    cout<<"Year "<<year<<"	"<<endl;
    for(month = 1; month <= 12; month ++)
    {
        cout<<"Month "<<month<<"	"<<endl;
        banker1.accountBalance = (banker1.accountBalance * (1 + monthlyInterestRate1)) + monthlyDepositAmount - monthlyWithdrawalAmount;
	cout<<"Balance for first account: "<<banker1.accountBalance<<endl;

	banker2.accountBalance = (banker2.accountBalance * (1 + monthlyInterestRate2)) + monthlyDepositAmount - monthlyWithdrawalAmount;
	cout<<"Balance for second account: "<<banker2.accountBalance<<endl;

    }
    cout<<endl;
}

Thank you SO much! I am eternally grateful.

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.