C 贷款计算器

C++ Loan Calculator

本文关键字:计算器      更新时间:2023-10-16

我正在尝试编写C 的程序,该程序获得了首发贷款余额,年利率和每月付款,并打印了一个时间表,该时间表显示余额在每次之后的余额截止贷款的月份,或者直到60个月过去了。

//Calculates a loan payment schedule
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double startingBalance = 0.0;
double interestRate = 0.0;
double monthlyPayment = 0.0;
double monthlyBalance = 0.0;
double compountInterest = 0.0;
double balance = 0.0;
int month = 0;
cout << fixed << showpoint;
cout << setprecision(2);
cout << "Starting loan balance: " ;
cin >> startingBalance;  //User input starting loan balance
if (startingBalance <= 0){
    cout <<"nPlease enter a positive number: " ;
    cin >> startingBalance;
}
cout << "nAnnual interest rate: " ;
cin >> interestRate;  //User input interest rate
if ((interestRate <= 0) || (interestRate > 1)){
    cout <<"nPlease enter an interest rate ranging from 0 to 1: " ;
    cin >> interestRate;
}
cout << "nMonthly payment: " ;
cin >> monthlyPayment; //User input monthly payment
if (monthlyPayment <= 0){
    cout <<"nPlease enter a positive number: " ;
    cin >> interestRate;
}
startingBalance = balance;
cout << "nMonth t Balancen" << endl; //Outputs a schedule of payments
while ((balance > 0) || (month < 61))
{
    balance += (startingBalance * (interestRate/12));
    balance -= monthlyPayment;
    month = month++;
    cout << month << "t" << balance << "n";
}

    return 0;
}

我相信陈述正确,但结果我一直在得到这些陈述

Starting loan balance: 10000.00
Annual interest rate: 0.0525
Monthly payment: 500.00
Month     Balance
1         -500.000
2         -1000.00
3         -1500.00

等,直到61个月过去了。

startingBalance = balance;

看起来应该逆转的

while ((balance > 0) || (month < 61))

您的描述表明&amp; amp;将更合适