试图使工作储蓄计算器在C ++

时间:2019-02-22 22:09:38

标签: c++

这是一个有关我希望它在运行程序时的外观的示例:


输入每年要存入的金额:3000

输入您的储蓄目标:50000

输入利率百分比:4.2

您的储蓄目标是在13年后实现的。

您的余额将为50513.15欧元。


问题在于,在我以百分比写出利率并按ENTER键之后,程序立即停止或关闭。

代码如下:

#include <iostream>

using namespace std;

int main()
{


cout << "Enter how much you want to deposit each year: ";
int deposit;
cin >> deposit;
cout << "Enter your savings target: ";
int targetBalance;
cin >> targetBalance;
cout << "Enter the interest rate in percent: ";
float interestPercent;
cin >> interestPercent;
interestPercent /= 100;

int years = 0;
double balance = 0;

while (balance < targetBalance)
{
    double interest = balance * interestPercent;
    balance += interest;
    balance += deposit;
    cout << balance << "\n";  
    years++;

}

cout << "\nYour savings target is achieved after " << years << " years.\n";
cout << "Your balance will then be " << balance << " EUR.";

return 0;
}

0 个答案:

没有答案