Percentages in C++

时间:2019-05-19 03:53:05

标签: c++

So I have a program where I have a loan that needs to pay off. One of the requirements is that we need to type the intrest rate in it's percentage form rather than the decimal form.

#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;


int main()
{
    double loanamt = 0;
    double interest = 0;
    double interestrate = 0;
    double monthlypay = 0;
    double netpay = 0;
    double currentamt = 0;
    double yearlyrate = 0;
    double monthlyrate = 0;
    int month = 0;

    cout << "Please enter the total amount of your loan." << endl;
    cin >> loanamt;

    cout << "Now enter your interest rate per each year." << endl;
    cin >> yearlyrate;

    cout << "Now, please enter your monthly pay." << endl;
    cin >> monthlypay;

    monthlyrate = yearlyrate / 12;
    currentamt = loanamt;

    if (monthlypay < monthlyrate)
    {
        cout << "ERROR. PAY TOO LOW." << endl;
        system("pause");
        return main();
    }

    while ((currentamt != 0) || (currentamt <= 0))
    {
        month++;
        interest = currentamt * monthlyrate;
        netpay = monthlypay - interest;
        currentamt = currentamt - netpay;
        cout << "Month: " << month << endl;
        cout << "Interest: " << interest << endl;
        cout << "Balance: " << currentamt << endl;
        if (currentamt <= 0)
        {
            cout << "It will take " << month << " months to complete the payment of your loan." << endl;
            system("pause");
            return main();
        }
    }

    system("pause");
    return main();
    return 0;
}

Does anyone have any idea as to how I could go about changing the interest rate from percentage to a decimal? The code works fine if I type it in decimal form, but I really want to meet the requirement provided to me.

1 个答案:

答案 0 :(得分:0)

没关系。我想到了。简单的数学。只需将利息输入除以100。无论如何,谢谢您对我的帖子的兴趣!