### rates

this is what I have so far.

 ``123456789101112131415161718192021222324`` ``````#include using namespace std; int main() { int RATE_KWHr = 9.92; // Rate of KWHr in 2001 int NEW_KWHr = 20.34; // Rate of KWHr in 2015 double average; char again; // To hold the RATE_KWHr cout << "Enter Rate of Increase Guess (in %): "; cin >> RATE_KWHr; cout << "The Current rate would be: " << NEW_KWHr << endl; cout << "The desired rate should be: " << NEW_KWHr << endl; cout << "Would you like to try again (Y or N): "; cin >> again; while (again == 'Y' || again == 'N'); return 0; }``````

The Output should be:
Enter Rate of Increase Guess (in %):
The Current rate would be:
The desired rate should be:
Would you like to try again (Y or N):
Last edited on
First of all, code tags make posted code much easier to read. See http://www.cplusplus.com/articles/jEywvCM9/

 It is easy to project the new yearâ€™s rate from the old years rate, we can simply use the formula: `newYearRate = (1 + averageRateIncrease) * oldYearRate;` If we use a loop to do this for 14 years

I see no such loop.

Rate of KWHr in 2001 and in 2015 are facts that do not change.

You do have a loop:
`while (again == 'Y' || again == 'N');`
that could be written
 ``123`` ``````while (again == 'Y' || again == 'N') { }``````

The loop repeats nothing.
If 'again' is Y or N, then the loop will run forever, because nothing in the loop changes the 'again'.
If 'again' is neither Y nor N, then the loop runs 0 iterations.

so this is what I have so far. can you please help me a little bit. how do I put a formula for 14 years in a loop? Thanks

 ``1234567891011121314151617181920212223`` ``````#include using namespace std; int main() { double oldYearRate = 9.92; // Rate of KWHr in 2001 double newYearRate = 20.34; // Rate of KWHr in 2015 double averageRateIncrease; char again; newYearRate = (1 + averageRateIncrease) * oldYearRate; cout << "Enter Rate of Increase Guess (in %): "; cin >> oldYearRate; cout << "The Current rate would be: " << newYearRate << " cents/KWHr" << endl; cout << "The desired rate should be: " << newYearRate << " cents/KWHr" << endl; while (again == 'Y' || again == 'N'); { cout << "Would you like to try again (Y or N): "; cin >> again; } return 0; }``````
Last edited on
Hello hilal1990,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

That is a good start, but I have some suggestions.

 ``12`` ``````double oldYearRate = 9.92; // Rate of KWHr in 2001 double newYearRate = 20.34; // Rate of KWHr in 2015 ``````

These are fixed numbers that should not change and the names do not reflect what they are. Since you do not want to change these numbers it would be better to write it this way. And the variable names should better reflect what they are:

 ``123`` ``````constexpr double startRate = 9.92; // Rate of KWHr in 2001 constexpr double endRate = 20.34; // Rate of KWHr in 2015 double newearRate{}; // <--- initializes to zero. Always initialize your variables. ``````

The first two lines can not be changed by the program now. If you do the compiler will tell you that you did something wrong.

In addition to defining "newYearRte" you will need some other variables to use in your program. Always initialize your variables to something. Just defining `char again;` will cause the while loop you have to fail before it can ever run because "again" has no value at this point, just storage space. `char again{ 'Y' };` will work better.

These lines:

 ``123`` ``````newYearRate = (1 + averageRateIncrease) * oldYearRate; cout << "Enter Rate of Increase Guess (in %): "; cin >> oldYearRate;``````

Should be inside the while loop.

These lines

 ``12`` ``````cout << "The Current rate would be: " << newYearRate << " cents/KWHr" << endl; cout << "The desired rate should be: " << newYearRate << " cents/KWHr" << endl;``````

May be useful inside the while loop also.

Just some thoughts for now. I will have to load up what you have and see how it works.

Hope that helps,

Andy
Thanks for your help Hand Andy. Have you loaded up the program yet? I tried everything you told me but i am still stuck.
hilal1990 wrote:
I tried everything you told me but i am still stuck.

I don't see where you tried the loop as recommended by keskiverto above:
 If we use a loop to do this for 14 years

By the way, usually it is better to re-post your latest code in your new reply. If the code in previous posts is edited it makes it hard to see what has already been tried and what changes have been made.
closed account (48T7M4Gy)
This is not necessarily what's required because the question isn't clear with everything being a rate.

 ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include using namespace std; int main() { const double start_price = 9.92; // Rate of KWHr in 2001 const double end_price = 20.34; // Rate of KWHr in 2015 char again = 'Y'; // SET INITIAL CONDITIONS double new_price = start_price; double growth_rate = 0.0; while (again == 'Y' || again == 'y') { cout << "Enter Rate of Increase Guess (in %): "; cin >> growth_rate; new_price = start_price; for(int i = 0; i < 14; ++i) { new_price = (1 + growth_rate/100) * new_price ; } cout << '\n'; cout << " The guessed rate would be: " << new_price << " cents/KWHr\n"; cout << "The desired rate should be: " << end_price << " cents/KWHr\n"; cout << "Would you like to try again (Y or N): "; cin >> again; } return 0; }``````
 What we want to find out is, over the intervening 14 years, what is the average rate of increase of the cost of a KWHr, per year?

The average rate of increase can be solved analytically and exactly, with a single equation. Without any loops or guesses by the user.

What "they" really want to find out is, does the student understand the use of variables and loop constructs?
Topic archived. No new replies allowed.