I am having issues with figuring out this code.

Hello I this program that I am trying to get the information from a text to run in a function but I cant seem to figure out how to do so also my professor wanted me to implement these some changes to the program. Can some one please help me out. The question is:
Savings Account Balance
Write a program that calculates the balance of a savings account at the end of a three month 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.
Here are the changes that he wanted me to do: You will implement the program as described in the text with the following changes:

NOTE: You will get the inputs from a file. You WILL accept negative numbers (indicating withdrawals).

You will write a deposit function that will add deposits to a balance.

You will write a withdrawal function that will test if the amount to withdraw is more than the balance. If it is, then it will display a message to the user and ignore the withdrawal.

In addition, your main function will also keep track of all withdrawals and all deposits made. At the end of the three months it will list the balance, total deposits and total withdrawals.

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

int main()
{
double StartBal, // Starting Balance
Deposit, // Monthly deposits
Withdrawn, // Monthy withdrawls
TotDepo = 0, // Total dposits
TotalWd = 0, // Total withdrawls
MthStart = 0, // Monthly starting balance
MthInt, // Monthly interest
TotInt = 0, // Total Interest
MthEnd, // Monthly ending balance
AnnInt; // Annual Interest

// Ask user to enter the starting balance and the annual interest rate
cout << "This program calculates the balance of a\n"
<< "savings account at the end of a three month period.\n\n";
cout << "What is the starting balance of the account? ";
cin >> StartBal;
cout << endl;

cout << "Enter the annual interest as a decimal: ";
cin >> AnnInt;
cout << endl;

MthStart += StartBal;

// Iterate loop once every month
for (int Mth = 1; Mth <= 3; Mth++)
{

do
{ // Ask user to input deposits for the month
// and validate only positive integers
cout << "What is the total amount deposited\ninto "
<< "the account during month " << Mth << "? ";
cin >> Deposit;

if (Deposit < 0)
{
cout << "Invalid integer.\n"
<< "Deposits must be a positive integer.\n";
}

} while (Deposit < 0);

cout << endl;
TotDepo += Deposit;

do
{ // Ask user to input monthly withdrawal and validate only positive
// integers and Total deposits is greater than withdrawals
cout << "What is the total amount withdrawn\nfrom "
<< "the account during month " << Mth << "? ";
cin >> Withdrawn;

if (Withdrawn < 0 || Withdrawn > TotDepo)
{
cout << "Invalid integer.\n";

if (Withdrawn < 0)
{
cout << "Withdrawals must be a positive integer.\n";
}
else
{
cout << "The Balance must be greater than withdrawals.\n";
}
}

} while (Withdrawn < 0 || Withdrawn > TotDepo);

cout << endl;

// Accumulate total withdrawn
TotalWd += Withdrawn;

// Calculate monthly ending balance
MthEnd = MthStart + Deposit - Withdrawn;

// Calculate monthly interest
MthInt = ((MthStart + MthEnd) / 2.0) * (AnnInt / 12.0);

// Accumulate total Interest
TotInt += MthInt;

MthStart = MthEnd + MthInt;
}

// Display report
cout << "\n Quarterly Savings Account Report\n"
<< "------------------------------------\n";
cout << fixed << showpoint << setprecision(2);
cout << "Starting balance : $ " << setw(9) << StartBal << endl;
cout << "Total deposits : $ " << setw(9) << TotDepo << endl;
cout << "Total withdrawals : $ " << setw(9) << TotalWd << endl;
cout << "Total interest : $ " << setw(9) << TotInt << endl;
cout << "Final balance : $ " << setw(9) << MthStart << endl;
return 0;
Hello princekid,

A couple of things to get you started while I look at what you have.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.

I noticed in the instructions that the program will be getting input from a file. Post the input file or a link to the file or a fair sample of the file, maybe 10 lines, so everyone will be using the same information. Also knowing what the file looks like helps when writing the code to read the file.

Andy
Hello princekid,

To start with let me make some observations, hints and suggestions.

I will not go into detail, but using namespace std; is not the best idea. I also realize that you may have been told to use this for a reason. Wish I could understand that. You can always do a search here on "using namespace std" and you should find many posts covering the topic.

In "main" you start be defining your variables. Some you have initialized and some you have not. My opinion is to initialize all variables to at least have the piece of mind that none of the variables contain a garbage value. And it is true that some variables do not need initialized like "StartBal" because the next lines get a value for this variable, i.e., it gets a value quickly. Since C++11 standards on initializing a variable is so easy, why not, e.g., "startBal{}". The empty {}s will initialize, in this case the double, to 0.0. For an "int" it would be zero.

Also it is best to start a regular variable with a lower case letter and camelCase is most often used when writing a variable name. So "StartBal" would become "startBal". Starting a name with a capital is generally used with classes and structs. My personal choice is to use a capital to start a function although this is not necessary.

In the section where you enter the "annual interest rate". This is a time to think about the user that is entering the number. Some people will have no idea the "2.25%" is the same as "0.0225" or even how to enter the proper decimal number. I would allow the user to enter "2.25" as a percentage and then divide this by 100 and put the result in "AmtInt" for later use.

Everything inside the for loop appears to be doing its job properly.

In the "display report" section I offer this as what could be done:
1
2
3
4
5
6
7
8
9
10
11
// Display report
std::cout << "\n Quarterly Savings Account Report\n";
std::cout << std::setfill('-') << std::setw(34) << '-' << std::setfill(' ') << '\n';
	//<< "------------------------------------\n";
std::cout << std::fixed << std::showpoint << std::setprecision(2);

std::cout << std::setw(22) << "Starting balance : $ " << std::setw(9) << StartBal << std::endl;
std::cout << std::setw(22) << "Total deposits : $ " << std::setw(9) << TotDepo << std::endl;
std::cout << std::setw(22) << "Total withdrawals : $ " << std::setw(9) << TotalWd << std::endl;
std::cout << std::setw(22) << "Total interest : $ " << std::setw(9) << TotInt << std::endl;
std::cout << std::setw(22) << "Final balance : $ " << std::setw(9) << MthStart << std::endl;

Line 3 is not a requirement, but to show you what could be done.

Lines 7 - 11 the first "setw()" helps to line up the output better. I do this because I am picky and from the experience I have. I think if you give it a test you will like it better.

That said and moving on to the changes.

NOTE: You will get the inputs from a file. You WILL accept negative numbers (indicating withdrawals).
I would start with this and decide if you want to open the file in "main" or in the function that uses it. Part of this will depend on how you read the file. Either one line at a time or the whole file putting the information into some container for later use. I would help to know what the contents of this file are.

I would work on this first because until you can get information to work with the other functions could be started, but you will have nothing to pass to these functions to work with.

Hope that helps,

Andy
Topic archived. No new replies allowed.