confusion with For loops

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Programmed by Jett Bailes
// 02.03.10
// ass2.cpp : main project file.
// calculate annual return on an investment

#include "stdafx.h"
#include <iostream>
using namespace std;

int main ()
{
	// Declare variables and constants.
	int  years;
	double investment, total;
	const double RATE = 0.08;
	const int 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.
Don't you also want to keep track of your accumulated interest inside your for loop?
The program won't stay up, why?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Programmed by Jett Bailes
// 02.03.10
// ass2.cpp : main project file.
// calculate annual return on an investment

#include "stdafx.h"
#include <iostream>
using namespace std;

int main ()
{
	// Declare variables and constants.
	int  years;
	double investment, total;
	const double RATE = 0.08;
	const int 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;
Last edited on
You might be right but I still can't get the program to stay up long enough for me to read it.
use system("PAUSE");

or something like

1
2
3
4
5
6
7
8
9
int main()
{
int i = 1;
std::cout << i << std::endl;

char f;
cin >> f;
return 0;
}
closed account (jwC5fSEw)
Nono, not system("PAUSE");. There is a large discussion about this here: http://www.cplusplus.com/forum/beginner/1988/

My personal favorite is cin.get(), though my current IDE (Code::Blocks) stays open automatically, which is nice.
Topic archived. No new replies allowed.