2014-01-29 8 views
0

Я пытаюсь написать программу на C++, которая берет стартовый баланс ссуды, годовую процентную ставку и ежемесячный платеж и печатает график, который показывает, какой баланс будет после каждого месяца, пока кредит не будет погасили или до 60 месяцев.C++ Loan Calculator

//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 месяцев не прошло.

+2

Compile со всеми предупреждениями и отладочной информации ('г ++ -Wall -g') , Научитесь использовать ** отладчик ** (возможно, 'gdb') –

+1

Вы никогда не устанавливали начальный баланс на сумму кредита. – OldProgrammer

ответ

1
startingBalance = balance; 

, который выглядит, как она должна быть обращена вспять

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

ваше описание предполагает, что & & будет более подходящим