Calculating interest and making nested loops not working

Hi guys/gals,

I am having an issue with a project I am working on for class. I manage to get everything else working as required but the last part is just killing me.

Here is what I need to get done:
For each quarter, calculate and display the beginning principal balance, the interest earned, and the final principal balance for the quarter.
For example: The user entered 1000.00 for the beginning principal balance, 5.25 for the interest rate, and 8 for the number of quarters. The output from this part of the program should be similar to the following:
Q| Beginning Principle| Interest Earned| End Principle
1| $1,000.00 | $13.13 | $1,013.13
2| $1,013.13 | $13.30 | $1,026.42
3| $1,026.42 | $13.47 | $1,039.89
etc

Here is the code I have so far, and I just am not quite sure where to go next.
Thanks guys for any help you can give me.

Code:
{
   cout << "Quarters" << "\t" << "Beginning Principles" << "\t" <<"Interest Earned" << "\t" <<"End Principal" << endl;
   endprin = balance + (quarter * interest);
   interest = quarter * interest;

   cout << endprin << endl;

   }


Last edited on
FYI the user inputs the number for every value, including the number of quarters, so I have to take that into account to go through the sequence a certain number of time based on what the user inputs.
what you need is a for loop

for (statement 1; statement 2; statement 3) {}
statement 1 is to initialise a variable to count with
statement 2 is like the argument in a while loop. the loop proceeds while it's true.
statement 3 is to increment the count variable

so this:
1
2
3
4
for (int x=0; x!=10; x++)
{
    cout << (x+1) << '\n';
}

will run through the loop ten times. (until (x!=10) returns false)

you'll need to make one like this:

EDITED
1
2
3
4
5
6
7
8
9
10
11
12
cout << "Q|\t" << "Beginning\t" << "Interest\t" << "End Principle|\n"
      << "  \tPrinciple\t" << "Earned\n";
for (int quantum=0; quantum < quarters; quantum++)
{
    end_principle = beginning_principle + (beginning_principle * interest);
  // above line maybe should use (interest / 4) if interest is ANNUAL rate
    cout << (quantum+1) << "|\t" << beginning_principle
          << "\t" << (beginning_principle * interest) // see above ^ for interest
          << "\t" << end_principle;
    beginning_principle = end_principle; // < this is needed to update beginning
                                         // principle for the next quarter..
}


for loops are enormously useful when you need to use the "counter", ("quantum" in this case) to access all the values in an array, or, even cooler, a vector (in which case the 'statement #2' would be something like "quantum < vector_name.size();"

in this case it's nice because the user can input the number of quarters they want to repeat, and use that variable to compare with your counter "quantum" or whatever you name it.
Last edited on
Topic archived. No new replies allowed.