我为一项任务创建了下面的程序。您应该可以输入您的原始本金、最低利率、最高利率和累积利息的年数。
测试后,一切正常,除非我输入我的最大兴趣为 .06、.6 等。由于某种原因,它不会显示最大兴趣。如果我输入 0.06 作为最低利息,它会显示成功,如果我使用高于 0.06 的最高利息金额,它将显示 0.06 金额。没有其他金额给我带来问题。
任何人都可以帮助我弄清楚我需要改变什么来纠正这个问题吗?
#include <iostream>
#include <iomanip>
#include <cmath>
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;
}