C++ 如何根据我的代码正确设置此贷款等式?

C++ 如何根据我的代码正确设置此贷款等式?,c++,visual-studio-2017,expression,formula,C++,Visual Studio 2017,Expression,Formula,这个问题需要我创建两个场景 用户接受经销商的折扣优惠,并通过其当地信用社为汽车提供资金 及 用户拒绝经销商的折扣优惠,但接受经销商较低的融资利率 它希望我使用定期付款公式,即:本金*利率/(1–(利率+1)-期限),并使用它获得月度或年度付款 我认为我的代码存在的问题与我用来获得年度或月度贷款付款的方程式有关,它肯定不能给我输入的正确答案,我不知道为什么 我试着改变了几次方程式,但仍然没有用 int main() // these are the variables I will be us

这个问题需要我创建两个场景

  • 用户接受经销商的折扣优惠,并通过其当地信用社为汽车提供资金
  • 用户拒绝经销商的折扣优惠,但接受经销商较低的融资利率
  • 它希望我使用定期付款公式,即:本金*利率/(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;
    
    
    }
    
    intmain()
    //这些是我将在这个项目的数学中使用的变量
    双倍年费;//每年计算完后将显示什么
    双月付款;//每月计算完后,将显示什么
    双原理;//借款金额
    双倍费率;//利率
    双mterm;//用户将为每月学期输入的内容
    双Y形;//什么用户将输入年度学期
    两年;//贷款期限(年)
    双月;//贷款期限(每月)
    双平方月;//贷款期限sqrt(每月)
    双平方年;//贷款期限sqrt(年)
    字符选择;
    }
    {
    库特原理;
    利率;
    选择;
    if(choice='M')//如果
    mterm=12;//一年内有12个月
    cout years;//我需要这些信息才能得到准确的答案
    月=月*年;
    sqrdMonth=sqrt(month);//我需要为定期付款公式的月份求平方根
    monthlypayment=(prinicple*rate)/(rate);sqrdMonth;//这就是我的问题所在
    //^^^^^为什么要我用“;”来结束我的等式
    有几个错误吗

    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;
    
    最后这个

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

    我不知道你为什么有两个分号。我觉得没有意义,但我不确定公式应该是什么。你的问题中没有提到平方根,所以我不确定你为什么在这里包括一个分号。

    听起来你可能需要学习如何使用调试器来逐步完成代码。有了一个好的调试器,你可以执行一行一行地检查你的程序,看看它偏离了你的预期。如果你要做任何编程,这是一个必不可少的工具。进一步阅读:感谢你对if语句的帮助,我不想使用额外的分号,但是程序甚至不会运行,除非我把我喜欢的公式放在那里如果它是principal*rate/(1–(rate+1)-sqrt(term)),它会给我一个错误代码,说明a是必需的,所以我按照编译器的要求做了,它工作了,但它仍然给我错误的答案
    principal*rate/(1–(rate+1)-sqrt(term))
    是不正确的,因为您没有声明名为
    term
    的变量。您也没有名为
    principal
    的变量。我知道这些可能是打字错误,但我只能继续您键入的内容。也不要太逐字逐句地理解编译器错误消息。根据您所说的,我认为这是正确的公式
    monthlypayment=(prinicple*rate)/(1-(rate+1)-sqrt(mterm));
    但即使这看起来在数学上也是错误的,因为它们相互抵消,+1和-1。对不起,约翰,我是个初学者。但我确实犯了一个错误,将原则命名为“prinicple”,如果用户输入“m”或“y”,则术语是sqrdMonth或sqrdYear相对的我想如果你不能让它工作,那么就发布你的公式,从你的程序中剪切粘贴,以及准确的错误消息,再次从你的程序中剪切粘贴。
    monthlypayment = (prinicple * rate) / (rate); sqrdMonth;