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;
}
| |