如何根据我的代码正确设置此贷款方程式?

时间:2019-01-16 20:02:39

标签: c++ visual-studio-2017 expression formula

问题要我创建2个方案

  1. 用户接受经销商的折扣优惠,并通过其当地的信用合作社为汽车融资。

  1. 用户拒绝经销商的返利优惠,但接受经销商较低的融资率。

我希望我使用定期付款公式,即:本金*利率/(1 –(比率+ 1)-期限),并使用它来获得月度或年度付款。

我认为我的代码遇到的问题与我用来获取年度或每月贷款还款的方程式有关,它肯定不能为我的输入提供正确的答案,我也不知道为什么。

我尝试过多次更改方程式,但仍然无济于事。

int main()


// these are the variables I will be using in maths for this project
double annualpayment; // what will be displayed once all is calculated annually 
double monthlypayment; // what will be displayed once all is calculated monthly
double prinicple; // amount borrowed
double rate; // interest rate
double mterm; // what the user will enter for monthly term 
double yterm; // what user will enter for yearly term
double years; // term of loan (yearly)
double month; // term of loan (monthly)
double sqrdMonth; // sqrt of term of loan (monthly)
double sqrdYear; // sqrt of term of loan (yearly)
char choice;
}
{
    cout << "Enter your principle: " << endl; // total amount borrowing
    cin >> prinicple;

    cout << "Enter your your interest rate: " << endl; // interest rate on loan
    cin >> rate;

    cout << "Will this be (M)onthly or (Y)early payment? (enter y or m)"; // declaring if it will be a monthly or yearly payment
    cin >> choice;

    if (choice = 'M') // if its monthly 
        mterm = 12; // there are 12 months within a year
    cout << "How many years will this loan be for?" << endl;
    cin >> years; // I need this information for getting the exact
    month = mterm * years;
    sqrdMonth = sqrt(month); // I need to square root the months for the periodic payment formula
    monthlypayment = (prinicple * rate) / (rate); sqrdMonth; // this is where my problem is 
                                                //  ^^^^ why is it asking me to close my equation with a ';'
    cout << "Your monthly loan payment is: ";
    cout << monthlypayment;

    if (choice = 'Y')
        yterm = 1;
    cout << "How many years will this loan be for?" << endl;
    cin >> years;
    years = yterm * years;
    sqrdYear = sqrt(years); // I need to square root the years for the periodic payment formula
    annualpayment = (prinicple * rate) / (rate); sqrdYear; // this is where my problem is 
                                        // ^^^^ why is it asking me to close my equation with a ';'
    cout << "Your annual loan payment is: ";
    cout << annualpayment;


}

}

我希望用户输入贷款的本金,利率和期限,然后由编译器进行数学运算,然后输出正确的数字。我的实际结果是负数或无理数。

1 个答案:

答案 0 :(得分:2)

几个错误

if (choice = 'M') // if its monthly 
    mterm = 12; // there are 12 months within a year

首先要说的是

if (choice == 'M') // if its monthly 
    mterm = 12; // there are 12 months within a year

在C ++中,我们使用==测试是否相等,并使用=分配给变量。

更认真地考虑这个

if (choice == 'M') // if its monthly 
    mterm = 12; // there are 12 months within a year
cout << "How many years will this loan be for?" << endl;
cin >> years; // I need this information for getting the exact
month = mterm * years;

现在假设choice不是'M',您认为mterm的价值是什么?

答案是未定义。但是,您在公式的两行下方使用了变量。使用具有未定义值的变量是不好的。

在我看来,您需要重组代码以在if语句中包含更多语句

if (choice == 'M')
{
    mterm = 12; // there are 12 months within a year
    cout << "How many years will this loan be for?" << endl;
    cin >> years; // I need this information for getting the exact
    month = mterm * years;
    sqrdMonth = sqrt(month); // I need to square root the months for the periodic payment formula
    monthlypayment = (prinicple * rate) / (rate); sqrdMonth; // this is where my problem is 
                                            //  ^^^^ why is it asking me to close my equation with a ';'
    cout << "Your monthly loan payment is: ";
    cout << monthlypayment;
}

最后这个

monthlypayment = (prinicple * rate) / (rate); sqrdMonth;

我不知道你为什么要有两个分号。对我来说毫无意义,但我不确定公式应该是什么。在您的问题中,公式中未提及平方根,因此我不确定为什么在此处包括平方根。