【问题标题】:Calculating Cash On Cash Returning 0 for the result计算现金返还结果为 0
【发布时间】:2021-02-07 01:59:50
【问题描述】:

我正在创建一个类函数,它会在其中返回 Cash on Cash return,它为您提供一个百分比,并且是查看您是否获得良好 ROI 的关键指标。我创建了这个函数,但由于某种原因,它没有给我想要的答案,即“3%”或“50%。现金回报通常是(年现金流/投资现金总额)。它只是返回 0。

double cashOnCashReturn(int cashFlowMonthly, int buyingPrice, double downPaymentP, double closingCost, int repairs) {

    int fullMonth = 12;

    int yearlyCashFlow = cashFlowMonthly * fullMonth;

    double downPaymentPercentage = downPaymentP/100;

    int downPaymentCashInvested = downPaymentPercentage * buyingPrice;

    long closingCostTotal = closingCost * buyingPrice;

    int cashInvestment = downPaymentCashInvested + repairs + closingCostTotal;

    double cashOnCash = (yearlyCashFlow / cashInvestment) * 100;

    cout << fixed << setprecision(0);

    return cashOnCash;
}

我的主要功能如下

int main()
{   
    string address;
    int buyingPrice;
    int rent;
    int cashFlowMonthly;
    int downPayment;
    double closingCostPercent = 0.05;
    int repairCost;

    // Mortgage Calculator Variables
    // double annualInterestRate;
    // double loanAmount;
    // double monthlyInterestRate;
    // double numberOfPayments;
    // double totalPayBack;
    // double monthlyPayment;

    cout << "Address: ";
    getline(cin,address);
    cout << "Buying Price: ";
    cin >> buyingPrice;
    cout << "Down Payment Percentage: ";
    cin >> downPayment;
    std::cout << "Repair Cost: ";
    std::cin >> repairCost;
    cout << "Rent Monthly: ";
    cin >> rent;
    std::cout << "Cashflow Monthly: ";
    std::cin >> cashFlowMonthly;

    realEstate firstHome;
    firstHome.setAddress(address);
    firstHome.setBuyingPrice(buyingPrice);
    firstHome.setRent(rent);
    
    std::cout <<"\n";
    std::cout << "================================================" << std::endl;
    std::cout << "Real Estate Calculator Created By Austin Nguyen" << std::endl;
    std::cout << "================================================" << std::endl;
    std::cout << "Address: " << firstHome.getAddress() << std::endl;
    std::cout << "\n";
    std::cout << "Buying Price: " << firstHome.getBuyingPrice() << std::endl;
    std::cout << "\n";
    std::cout << "Rent: " << firstHome.getRent() << std::endl;
    std::cout << "\n";
    std::cout << "Does It Pass One Percent Rule? " << firstHome.onePercentRule(buyingPrice,rent) << std::endl;
    std::cout << "\n";
    std::cout << "Cash On Cash Return: " << firstHome.cashOnCashReturn(cashFlowMonthly,buyingPrice,downPayment,closingCostPercent,repairCost);
    std::cout << "\n";
    std::cout << "================================================" << std::endl;
    std::cin.clear();

【问题讨论】:

  • 请提供一个典型的调用(带值)
  • @SuperSymmetry 这是我的第一个项目,如果我的代码草率且杂乱无章,非常抱歉。我在下面的调用中添加了我的主要功能。
  • 您可以将yearlyCashFlowcashInvestment 的数据类型更改为double,这应该可以解决问题。
  • @YangYushi 工作如神,感谢急需帮助的兄弟。你能解释一下为什么它应该是双倍的吗?我以为双数是 0.01、0.20、3.5?

标签: c++ c++11


【解决方案1】:

yearlyCashFlowcashInvestment 是整数。将两个整数相除会得到一个整数 - 即使将结果分配给一个双精度数。

ala double y = 3 / 5 导致y 被赋值为零,因为3/5 是一个整数表达式,它被评估为0 而不是.6.。但是3/5.0 是一个浮点表达式。

所以这一行:

double cashOnCash = (yearlyCashFlow / cashInvestment) * 100;

应该是:

double cashOnCash = (yearlyCashFlow / (double)cashInvestment) * 100;

将除法表达式中的一个元素从 int 转换为 double 会导致整个表达式被计算为浮点表达式。

【讨论】:

  • 啊!!!!我现在明白了,非常感谢您的帮助。我被这个问题困扰了好几个小时。
猜你喜欢
  • 1970-01-01
  • 2020-02-26
  • 1970-01-01
  • 2020-06-28
  • 2014-07-22
  • 1970-01-01
  • 2022-11-03
  • 2012-08-30
相关资源
最近更新 更多