Am I looping wrong??

I've been working on this code for the last day. Although it works, I need a clue on how I can get the loop to keep repeating....any suggestions.

Code Below:

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

int main ()
{
int i;
int yrs;
float peryr;
float total;

// Prompt user to enter number of years it took to graduate
cout << "Enter the amount of years it took to graduate: ";
cin >> yrs;

double tuition = 10000;

for (int i = 1; i < yrs; i++)
{
peryr = tuition + tuition * 0.05;
if (tuition < peryr)
{
peryr = peryr + peryr * 0.05;
}
}
cout << "The amount of tuition for the year is " << peryr<< "\n";
system ("pause");
}
Are you looking to repeat the entire program each time you're program reaches its end? Also, you're missing return 0;
It's supposed to compute the tuition after an increase of 5% every year. So if I input 9 years, it's supposed to tell how much tuition was each year (1-9). Well not really year 1 b/c it's 10000 during that year. I was told I didn't need the return 0 unless I was using Visual C++. But thanks, I'll insert it.
Hello. Here are a few things about your code:
* You're not using float total at all. This variable isn't useful to you here, so I'd delete it?
* You declare int i; in the scope of main, but then redeclare it in the for loop. You'll want to use one or the other. Right now you're declaring i at the beginning of the function, and then hiding it with the one you make for the for loop. In this instance I'd just delete the one at the beginning.
* What does peryr mean? It may mean something to you, but a Google for it did nothing for me. If it isn't going to be obvious to the users of this program (Common lingo for the problem domain) then I would rename it to something easier to read. *Just realized it means per year..., but I'm leaving what I said*
* You're mixing float and double. Is there a reason for that?
* You have #include <string> but you aren't using any strings. I'd remove that line because it does you no good. Include the headers you need, and no more.

You also have a bug where you're resetting the value of peryr to tuition + tuition * 0.05 every time through the loop. That means that tuition will always be the same value when you get to the if statement, and you'll modify it again in the same way. So although you thought it wasn't looping, it was. It was just resetting the value every time. Fixing that will fix your program.

Since I went ahead and said what the bug is, I'll go ahead and give a version of this program I wrote which may be an example of the changes I was talking about. Lemme know what you think.

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
44
#include<iostream>
using namespace std;

int main ()
{
	// The rate at which the tuition payment each year increases.
	const double TUITION_RATE = 1.05;

	// Prompt the user.
	cout << "                Tuition Calculator" << endl
		<<	"-------------------------------------------------------------" << endl
		<<	"Tution for your first year is $10,000. Every year after that cost" << endl
		<<	"5% more than the last year." << endl
		<<	"Please enter the total number of years you took to graduate:";
	
	int years;
	cin >> years;

	// For formatting.
	cout << endl << endl;

	if (years < 1)
	{
		cout << "It's too early to use this program." << endl;
		return 0;
	}

	// The total tuition, and the amount by which it will increase next.
	double totalTuition = 10000;
	double nextIncrease = totalTuition * TUITION_RATE;

	// We went ahead and set the tuition for the first year. The loop only handles years after that.
	for (int i = 1; i < years; ++i)
	{
		// Add nextIncrease to totalTuition.
		totalTuition += nextIncrease;
		// Recalculate nextIncrease in case we're not done.
		nextIncrease *= TUITION_RATE;
	}

	cout << "Your total tuition paid is: " << totalTution << endl;

	return 0;
}
#include<iostream>
#include<string>
using namespace std;

int main ()
{
int i;
int yrs;
float peryr;
float total;

// Prompt user to enter number of years it took to graduate
cout << "Enter the amount of years it took to graduate: ";
cin >> yrs;

double tuition = 10000;

for (int i = 1; i <= yrs; ++i)
{
cout << "The amount of tuition for year "<<i <<" is $" <<tuition <<"." <<endl;
tuition += tuition * .05;
}

return 0;
}

This to my understanding gets good results. The reason the cout << "The amount of tuition for the year is " << peryr<< "\n"; wasn't being output is because it wasn't in the loop. Unless there's some other reason you need the peryr variable, tuition += tuition + tuition * .05; is concise and functions properly by adding 5% to each year and saving it as tuition. Hope this helps!
Thanks a bunch for your responses. I'm going to try your suggestions and let you all know how it went.....Thanks Again.
Both loops work great!! Thanks again.
Topic archived. No new replies allowed.