Here's my code... i put in some troubleshooting couts, but I can't figure out what's going on.

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
    const double INT_RATE = 0.10 ;
    const double FACTOR = 1.0 * INT_RATE ;
    const int START_AGE = 16 ;
    const int STOP_AGE = 60 ;

    int age ;
    double begPrin ;

    double newPrin ;
    double newPrin1 ;
    double intP1 ;
    double monPay ;

    ofstream outFile ;

    cout << "Beginning Principle:  " << flush ;
    cin >> begPrin ;
    cout << endl << endl ;
    cout << begPrin << endl << endl ;

    newPrin = begPrin ;
    cout << newPrin ;

    for ( age = START_AGE ; age <= STOP_AGE ; age++ )
        newPrin *= (1.0 + INT_RATE) ;
    cout << endl << endl << endl << newPrin ;

    intP1 = newPrin1 - newPrin ;
    monPay = intP1 / 12 ;
    newPrin1 = newPrin * FACTOR ;

    cout << fixed << showpoint << setprecision(2) ;
    outFile << fixed << showpoint << setprecision(2) ;

    cout << "The initial investement was $" << begPrin << ".  The total amount accumulated after " 
         << STOP_AGE - START_AGE << " years, if $" << begPrin << " is allowed to compound with an intereste of "
         << INT_RATE * 100 << "%, comes to $" << newPrin << "." << endl << endl ;
    cout << "The total amount accumulated after " << (STOP_AGE - START_AGE) + 1 << " (years + 1) years, if $" 
         << begPrin << " is allowed to compound with an interest of " << INT_RATE * 100 << "%, comes to $"
         << newPrin << endl << endl ;
    cout << "The intereste earned during this year is $" << intP1 << ".  If interest is withdrawn each year thereafter, your income is $" << monPay << " per month." << endl << endl ;

    outFile.open(" wishfulthinking.txt ") ;

    outFile << "The initial investement was $" << begPrin << ".  The total amount accumulated after " 
            << STOP_AGE - START_AGE << " years, if $" << begPrin << " is allowed to compound with an intereste of "
            << INT_RATE * 100 << "%, comes to $" << newPrin << "." << endl << endl ;
    outFile << "The total amount accumulated after " << (STOP_AGE - START_AGE) + 1 << " (years + 1) years, if $" 
            << begPrin << " is allowed to compound with an interest of " << INT_RATE * 100 << "%, comes to $"
            << newPrin << endl << endl ;
    outFile << "The intereste earned during this year is $" << intP1 << ".  If interest is withdrawn each year thereafter, your income is $" << monPay << " per month." << endl << endl ;

return 0 ;
}

Recommended Answers

All 3 Replies

You need to be a bit more specific. What did you expect from your code and what kind of behaviour is it displaying as of now...

Oh and btw, taking a look at your code, I see that you have used floating point variables as your loop variables which is as such deemed to show behaviour you least expected. This is because of the way floating point numbers are internally represented. Read this.

Consider switching to an integer loop variable.

You need to be a bit more specific. What did you expect from your code and what kind of behaviour is it displaying as of now...

Oh and btw, taking a look at your code, I see that you have used floating point variables as your loop variables which is as such deemed to show behaviour you least expected. This is because of the way floating point numbers are internally represented. Read this.

Consider switching to an integer loop variable.

I'm not sure which loop variables you are referring to.
Here is my modified code, which looks like it is giving a valid output.
The code is supposed to do the following:

1) Find the final balance after 'x' amount of years with a 'y' compound interest rate

2) After 'x' years + 1, find the interest earned that year

3) Find the amount alloud to withdrawl each month (if only extracting from interest earned that year)

I was originally getting numbers with an e-0.044 for newPrin and whatnot... I posted a current sample of what my output looks like (slightly modified for printing purposes).

*****************************************************
* COSC 230 - Structured Programming
* Chapter 5:  Programming Exercise #12
* March 8, 2007
*
* Discription:
*    Find the principle after 'x' amount of years with a compound
*        interest rate of 'y'.
*    Find monthly pay off one years interest after 'x' + 1 years
******************************************************/

//header files
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
    //variables
    const double INT_RATE = 0.10 ;
    const int START_AGE = 16 ;
    const int STOP_AGE = 60 ;

    int age ;        //control variable

    double begPrin ;
    double newPrin ;    //prevent loss of data from begPrin
    double newPrin1 ;    //prevent loss of data from newPrin
    double intP1 ;    //interest of newPrin1
    double monPay ;

    ofstream outFile ;

    cout << "Beginning Principle:  " << flush ;
    cin >> begPrin ;
    cout << endl << endl ;

    newPrin = begPrin ;

    for ( age = (START_AGE + 1); age <= STOP_AGE ; age++ )    //age + 1: counter starts at 0
        newPrin = newPrin + (newPrin * INT_RATE) ;

    newPrin1 = newPrin + (newPrin * INT_RATE) ;        //principle after STOP_AGE + 1
    intP1 = newPrin1 - newPrin ;        //interest of prin at STOP_AGE + 1 year
    monPay = intP1 / 12 ;

    cout << fixed << showpoint << setprecision(2) ;
    outFile << fixed << showpoint << setprecision(2) ;

    cout << "The initial investment was $" << begPrin << ".  The total amount accumulated after " 
         << STOP_AGE - START_AGE << " years, if $" << begPrin << " is allowed to compound with an interest of "
         << INT_RATE * 100 << "%, comes to $" << newPrin << "." << endl << endl ;
    cout << "The total amount accumulated after " << (STOP_AGE - START_AGE) + 1 << " (years + 1) years, if $" 
         << begPrin << " is allowed to compound with an interest of " << INT_RATE * 100 << "%, comes to $"
         << newPrin << endl << endl ;
    cout << "The interest earned during this year is $" << intP1 << ".  If interest is withdrawn each year thereafter, your income is $" << monPay << " per month." << endl << endl ;

    outFile.open(" wishfulthinking.txt ") ;

    outFile << "The initial investment was $" << begPrin << ".  The total amount accumulated after " 
            << STOP_AGE - START_AGE << " years, if $" << begPrin << " is allowed to compound with an interest of "
            << INT_RATE * 100 << "%, comes to $" << newPrin << "." << endl << endl ;
    outFile << "The total amount accumulated after " << (STOP_AGE - START_AGE) + 1 << " (years + 1) years, if $" 
            << begPrin << " is allowed to compound with an interest of " << INT_RATE * 100 << "%, comes to $"
            << newPrin << endl << endl ;
    outFile << "The interest earned during this year is $" << intP1 << ".  If interest is withdrawn each year thereafter, your income is $" << monPay << " per month." << endl << endl ;

return 0 ;
}

Beginning Principle: 1700
The initial investment was $1700.00. The total amount accumulated after 44 years, if $1700.00 is allowed to compound with an interest of 10.00%, comes to $112648.93.

The total amount accumulated after 45 (years + 1) years, if $1700.00 is allowed to compound with an interest of 10.00%, comes to $112648.93

The interest earned during this year is $11264.89. If interest is withdrawn each year thereafter, your income is $938.74 per month.
Press any key to continue . . .

> for ( age = (START_AGE + 1); age <= STOP_AGE ; age++ ) //age + 1: counter starts at 0
> newPrin = newPrin + (newPrin * INT_RATE) ;
If you want the rest of the code to be part of the for loop, then you need to use some { }

If you always use them, even when they are strictly optional, then you won't be tempted to forget about using them when it matters.

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.