C ++贷款计算器

时间:2014-01-29 00:59:58

标签: c++

我正在尝试用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 Balance\n" << 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个月过去了。

1 个答案:

答案 0 :(得分:1)

startingBalance = balance;

看起来应该颠倒

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

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