【发布时间】:2016-02-07 11:58:22
【问题描述】:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int principal = 1000;
int years = 1; //counter
double balance = 0.0;
double rate = .02;
do
{
cout << "Year " << years << ":" << endl;
while (rate < .05)
{
balance = principal * pow(1 + rate, years); //display rate with no decimal place
cout << fixed << setprecision(0);
cout << " Rate" << rate * 100 << "%: $";
cout << setprecision(2) << balance << endl; //display balance with two decimal places
rate += .01;
}
years += 1;
} while (years < 6);
system("pause");
return 0;
}
我有这个程序,可以按 2%、3% 和 4% 计算年利率,并显示 5 年以上的金额。我的 while 循环将在第一年运行并显示它,但不是第 2 年到第 5 年。我已经多次编辑了该程序,但似乎无法让它显示结果。对此的任何帮助将不胜感激。
【问题讨论】:
标签: c++ loops while-loop