You are using a version without Ads of this website. Please, consider donating:

### Interest Rate will not Display as .06, .6, etc.

I created the program below for an assignment. You are supposed to be able enter in your original principal, minimum interest rate, maximum interest rate, and number of years to accumulate interest.

After testing it, everything works just fine unless I input my maximum interest as .06, .6, etc. For some reason, it will not display the maximum interest. If I input .06 as the minimum interest, it displays successfully, and if I use a maximum interest amount higher than .06, it will display the .06 amount. There is no other amount that presents me with a problem.

Would anyone be able to assist me in figuring out what I need to change to correct this?

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````#include #include #include using namespace std; int main() { //variables & named constants int principal = 0; double minimumInterest = 0.0; double maximumInterest = 0.0; int yearBase = 1; int years = 0; double balance = 0.0; //inputs cout << "Enter the beginning principal: \$"; cin >> principal; cout << "Enter the minimum interest rate: "; cin >> minimumInterest; cout << "Enter the maximum interest rate: "; cin >> maximumInterest; cout << "Enter the number of years the interest will accumlate: "; cin >> years; //calculations & loop do { cout << "Year " << yearBase << ":" << endl; for (double rate = minimumInterest; rate <= maximumInterest; rate += .01) { balance = principal * pow(1 + rate, yearBase); //display rate with zero decimal places cout << fixed << setprecision(0); cout << " Rate " << rate * 100 << "%: \$"; //display balance with two decimal places cout << setprecision(2) << balance << endl; } //update years counter yearBase +=1; } while (yearBase <= years); system("pause"); return 0; } ``````
@DarrelLopez (1)

Don't make pointless posts like that, you could be reported to admin for trolling, and have your account closed.

@RedTail (1)

`for (double rate = minimumInterest; rate <= maximumInterest; rate += .01)`

Never use double in loop conditions like that - always use integers, then cas t them to doubles inside the loop. You will need more variables.

Doubles are represented as binary fractions and not all real numbers can be represented, so direct comparisons quite often fail. Consider this:

 ``123`` ``````float a = 0.1; // a == 0.09999997 float b = 10.0 * a; // b == 0.9999997 if (a == b) //fail - evaluates to false ``````

Changing the type to double does not solve the problem.

Hope this helps.
Topic archived. No new replies allowed.