I am trying to set up a 12 month table summary in a bank account program. The problem I have is I am trying to figure out how to set it up correctly. Here is my class and application from the bank account:

Here's the class part:

class SavingsAccount
{
	//Data declaration
	private:
		int accountNumber;
		double balance;
		double rate;
		string depositChoice;
		string withdrawalChoice;
		double deposit;
		double withdrawal;
		double endBalance;


	public:
		//Methods declarations/prototypes
		void setAccount(int,double,double);
		double getBalance();
		string deposChoice();
		string withdrawChoice();
		double depositBalance();
		double withdrawalBalance();
		double getEndingBalance();	
		
};

//Methods implementation

//Constructor method:  a special method used to 
//initialize an object's state
//The constructor is automatically called and executed
//when the object is instantiated

void SavingsAccount::setAccount(int accNo, double startBalance, double startRate)
{
	accountNumber = accNo;
	balance = startBalance;
	rate = startRate;
}
double SavingsAccount::getBalance()
{
	cout << "\nThe current balance in your account is $"
		<< balance << endl;

	return balance;
}
string SavingsAccount::deposChoice()
{
	cin.ignore();
	cout << "\nWould you like to make a deposit(Y/N): ";
	getline(cin, depositChoice);
	
	return depositChoice;
}
double SavingsAccount::depositBalance()
{
		if (depositChoice == "Y")
		{
			cout << "\nEnter amount to deposit:  ";
			cin  >> deposit;
			return deposit;
		}	
		else if (depositChoice == "N")
		{
			cout << "\nOk no deposit I see!" 
				<< endl;
		}	
}
string SavingsAccount::withdrawChoice()
{
	cin.ignore();
	cout << "\nWould you like to make a withdrawal(Y/N): ";
	getline(cin, withdrawalChoice);
	
	return withdrawalChoice;
}
double SavingsAccount::withdrawalBalance()
{
	if (withdrawalChoice == "Y")
	{
		cout << "\nEnter amount to withdrawal:  ";
		cin  >> withdrawal;
		return withdrawal;
	}	
	else if (withdrawalChoice == "N")
	{
		cout << "\nOk no withdrawal I see!" 
			<< endl;
	}
}

double SavingsAccount::getEndingBalance()
{
	if (depositChoice == "Y" && withdrawalChoice == "Y")
	{
		endBalance = balance + deposit - withdrawal;
		cout << "\nThe ending balance for your account is $"
			<< endBalance << endl;
		return endBalance;
	}
	else if (depositChoice == "Y" && withdrawalChoice == "N")
	{
		endBalance = balance + deposit;
		cout << "\nThe ending balance for your account is $"
			<< endBalance << endl;
		return endBalance;
	}
	else if (depositChoice == "N" && withdrawalChoice == "Y")
	{
		endBalance = balance - withdrawal;
		cout << "\nThe ending balance for your account is $"
			<< endBalance << endl;
		return endBalance;
	}
	else if (depositChoice == "N" && withdrawalChoice == "N")
	{
		endBalance = balance;
		cout << "\nThe ending balance for your account is $"
			<< endBalance << endl;
		return endBalance;
	}
}

Here's the cpp/application part:

#include <iostream>
#include<string>
#include <iomanip>
using namespace std;

#include "SavingsAccount.h"
//file is located in project directory

int main()
{
	cout << "Welcome to the bank today!" << endl;

	SavingsAccount newAccount;
	newAccount.setAccount(111, 4000.0, .004);
	newAccount.getBalance();
	newAccount.deposChoice();
	newAccount.depositBalance();
	newAccount.withdrawChoice();
	newAccount.withdrawalBalance();
	newAccount.getEndingBalance();

	cout << "\nMONTH	START BALANCE	INTEREST	END BALANCE\n";
	int month;
	double interest = 0.0;
	double endBalance = 0.0;
	double rate = 0.0;
	double newEndBalance = 0.0;
	month = 1;
	while (month <= 12)
	{
	  interest = endBalance * rate;
	  newEndBalance = endBalance + interest;
	  cout << month << endBalance << interest << newEndBalance <<    endl;
	  month++;
	}
	cout << "\nThanks for stopping by the bank, have a nice day!\n" << endl;
	return 0;	
}//END MAIN

The output of the table is supposed to be like this:

Month Starting Interest Ending
# Balance Earned Balance
1 4000.00 16.00 4016.00
2 4016.00 16.06 4032.06
3 ... ... ...
4
5
6
7
8
9
10
11
12
The start balance of is table is the ending balance after I deposited/withdrew funds, the interest part I have to multiply the balance to the rate(.004) and the end balance is where I have to add the balance to interst.

The table part of it is the last part of the cpp part, I just wanted to know what to put there to make it come out correctly. Thanks greatly for the help.

Dani AI

Generated

Quick diagnosis and the minimal fix
The table comes out wrong because the monthly loop is using uninitialized or incorrect values: rate and the loop endBalance variable in main() are never set from the account object, and the program never copies the account's ending balance (after deposit/withdraw) into the loop's starting balance. Also note that some of the member functions in 's class sometimes omit returning a value (undefined behavior) and that getBalance() prints as a side effect instead of simply returning a value. Capture one canonical starting balance (the account balance after any deposit/withdraw), use that to compute interest, then set the next month’s start = that month’s end.

Practical loop (drop this into your main() after you have the final post-deposit balance and a monthly rate):

double start = account.getBalance();        // balance after deposit/withdraw
double monthlyRate = account.getInterestRate(); // if stored as annual, do monthlyRate = annual/12
cout << fixed << setprecision(2);
cout << "Month   StartBalance   Interest   EndBalance\n";
for (int m = 1; m <= 12; ++m) {
    double interest = start * monthlyRate;
    double endBal   = start + interest;
    cout << setw(2) << m << setw(15) << start << setw(12) << interest << setw(12) << endBal << '\n';
    start = endBal;                            // carry forward to next month
}

Class & input hygiene
Make getBalance() return the value only (no printing). Add getInterestRate() and make deposit() / withdraw() update the internal balance and always return the amount (or 0.0). Use char answers + toupper() for Y/N, or validate string input, and always initialize member variables in the constructor. For display use fixed << setprecision(2) and setw for aligned columns. If strict money accuracy is required, consider representing cents as integers instead of double.

Notes on thread contributions
was right that a simple loop prints the table; the missing piece is carrying the ending balance into the next iteration. ’s output bug looks like a print/formula mix-up (printing + INT_RATE instead of multiplying); computing interest into a separate variable then printing that variable avoids this class of mistake.

Recommended Answers

All 3 Replies

You just want to know how to print a table? I did it using three for loops, kinda like this:

cout<<"\n";
            for(int x=0 ; x<=3; x++){//prints rows of 4 incremented rates       
                cout<<"\trate"; //print one row, then increment the rate

            }//end for 

            cout<<endl;

            for(int y = 1; y<=12; y++){//makes 12 rows
                cout<<"\nYear "; //beginning of rows, 1st column

                for(int z=0; z<=3;z++){//print rows, 2nd - 4th columns
                //calculate the factor
                    cout<<"\tfactor";

                }//end for z 
            }//end for y
        cout<<endl;

You just want to know how to print a table? I did it using three for loops, kinda like this:

cout<<"\n";
            for(int x=0 ; x<=3; x++){//prints rows of 4 incremented rates       
                cout<<"\trate"; //print one row, then increment the rate

            }//end for 

            cout<<endl;

            for(int y = 1; y<=12; y++){//makes 12 rows
                cout<<"\nYear "; //beginning of rows, 1st column

                for(int z=0; z<=3;z++){//print rows, 2nd - 4th columns
                //calculate the factor
                    cout<<"\tfactor";

                }//end for z 
            }//end for y
        cout<<endl;

Oh I know how to print the table, I'm just having trouble on how to print out the starting balance, the interest, and ending balance right. It's kind've difficult to really know what I mean because some of the program came from the class which is seperate from the application/cpp file.

I am working on the exact same problem and having the same issue. I can't seem to get the ending balance for the previous month to be the starting balance for the next month. Here's my code:

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

#include "SavingsAccount.h"

int main()
{
    //Create one instance of a SavingsAccount
    SavingsAccount myAccount("Z1234A", 4000.0, 0.004);
    double currentBalance;
    char makeDeposit = ' ';
    double amount = 0.0;
    char makeWithdrawal = ' ';
    double INT_RATE = .004;
    double startingBal = 0.0;
    double endingBal = 0.0;

    //Set decimal spaces to 2
    cout << fixed << setprecision(2);

    string accountNumber;
    accountNumber = myAccount.getAcctNo();

    //Display the name of software and company
    cout << setw(55) << " BANKING SOLUTIONS SOFTWARE 1.0 " << endl;
    cout << setw(54) << "Prioleau Banking Corporation " << endl << endl;

    //Welcome the customer
    cout << setw(61) << "Welcome to the Prioleau Banking Corporation!" << endl << endl;

    //Display the current state of the account including
    //account number, current balance and interest rate 
    accountNumber = myAccount.getAcctNo();
    cout << "Your account number is: " << accountNumber << endl;

    currentBalance = myAccount.getBalance();
    cout << "Your current balance is: $" << currentBalance << endl;

    double startingRate;
    startingRate = myAccount.getInterestRate();
    cout << "The current interest rate is: " << ".4%" << endl << endl;

    //Customer makes a deposit
    cout << "Will you be making a deposit today (Y or N)?" << endl;
    cin >> makeDeposit;
    cout << endl;

    if (toupper(makeDeposit) == 'Y')
    {
        cout << "Please enter the amount that you would like to deposit." << endl;
        cin >> amount;
        cout << endl;

        myAccount.addToBalance(amount);
        currentBalance = myAccount.getBalance();

        //Display the ending state of the account
        cout << "Your current balance is now: $" << currentBalance << endl << endl;
    }
    else
        cout << "Thank you." << endl << endl;

    //Customer makes a withdrawal
    cout << "Will you be making a withdrawal today (Y or N)?" << endl;
    cin >> makeWithdrawal;
    cout << endl;

    if (toupper(makeWithdrawal) == 'Y')
    {
        cout << "Please enter the amount that you would like to withdraw." << endl;
        cin >> amount;
        cout << endl;

        myAccount.withdrawFromBalance(amount);
        currentBalance = myAccount.getBalance();

        //Display the ending state of the account
        cout << "Your current balance is now: $" << currentBalance << endl << endl;
    }
    else
        cout << "Thank you." << endl << endl;

    //Display a 12-month summary of the account that shows the effect
    //of the current interest rate on the growth of the balance
    //assuming that the interest is applied as simple interest once per month
    cout << "As of today's balance, here is a 12-month summary of the account that "<<
            "shows the effect of the current interest rate on the growth " << 
            "of the balance; assuming that the interest is applied as simple interest "<<
            "once per month." << endl << endl << endl;

    //Headers
    cout << "Month" << setw(20) << "Starting" << setw(20) << "Interest" << setw(20)
             << "Ending" << endl;
    cout << "  #" << setw(21) << "Balance" << setw(19) << "Earned" << setw(23)
             << "Balance" << endl;

    //line number variable for print out
    int lineNum = 1;

    //Formula for ending balance
    endingBal = currentBalance + (currentBalance * INT_RATE);

    //12 month interest report output line 1        
    cout << setw(3) << lineNum << setw(21) << currentBalance + INT_RATE << setw(18) << currentBalance * INT_RATE 
        << setw(24) << endingBal;
        cout << endl;

    //Formula for starting balance
    startingBal = endingBal;    

    while (lineNum <= 11)
    {
        //Line number counter for 12 month interest report output
        lineNum += 1;

        //12 month of interest report output
        cout << setw(3) << lineNum << setw(21) << endingBal + INT_RATE << setw(18) << startingBal * INT_RATE 
            << setw(24) << endingBal;
            cout << endl;

    }
    //end while

    cout << endl << endl;
    cout << "Thank you for choosing Prioleau Banking Corporation. Have a wonderful day!" << endl;

    cout << endl << endl;
    system ("pause");
    return 0;
}
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.