I'm trying to calculate an annual interest return on an investment. I need the program to display the text for each year showing incrementally how much interest is earned annually without increasing the investment.
// Programmed by Jett Bailes
// 02.03.10
// ass2.cpp : main project file.
// calculate annual return on an investment
#include "stdafx.h"
#include <iostream>
usingnamespace std;
int main ()
{
// Declare variables and constants.
int years;
double investment, total;
constdouble RATE = 0.08;
constint MAX_YRS = 30;
// User enters data.
cout << "Enter investment amount ";
cin >> investment;
total = investment;
cout << "Enter the term in years ";
cin >> years;
//do not allow the number of years less than 1 or more than 30.
while (years < 1 || years > MAX_YRS)
{
cout << "You must enter a value between 1 and " << MAX_YRS << endl;
cout << "Please re-enter the term in years ";
cin >> years;
}
//calculate and output results
cout << "/n";
for (int num = 1; num <= years; ++num)
{
double annual;
annual = total * RATE;
cout << "The annual return on year " << num << " is $" << annual << endl;
}
return 0;
}
Everything up until the for statement is okay, I just can't seem to keep the program running, or calculate correctly.
// Programmed by Jett Bailes
// 02.03.10
// ass2.cpp : main project file.
// calculate annual return on an investment
#include "stdafx.h"
#include <iostream>
usingnamespace std;
int main ()
{
// Declare variables and constants.
int years;
double investment, total;
constdouble RATE = 0.08;
constint MAX_YRS = 30;
// User enters data.
cout << "Enter investment amount ";
cin >> investment;
total = investment;
cout << "Enter the term in years ";
cin >> years;
//do not allow the number of years less than 1 or more than 30.
while (years < 1 || years > MAX_YRS)
{
cout << "You must enter a value between 1 and " << MAX_YRS << endl;
cout << "Please re-enter the term in years ";
cin >> years;
}
//calculate and output results
cout << "/n";
for (int num = 1; num <= years; num++)
{
total += investment;
cout << "The annual return on year " << years << " is $" << total << endl;
}
return 0;
}
You need to keep annual = total * RATE;
(line 39) from the first post.
You need to keep total += investment;
(line 38) from your second post.
It would be a good idea though to put this: double annual;
outside the for loop.
cout << "The annual return on year " << years << " is $" << total << endl;
needs to be: cout << "The annual return on year " << num << " is $" << total << endl;
The slash is wrong on this line: cout << "/n";
It should be: cout << "\n";
This is why the calculation is wrong. total += investment;
should be total += annual;
At the beginning when you ask the user for their investment, you should just store it in total. cin >> total;