【发布时间】: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 这是我的第一个项目,如果我的代码草率且杂乱无章,非常抱歉。我在下面的调用中添加了我的主要功能。
-
您可以将
yearlyCashFlow或cashInvestment的数据类型更改为double,这应该可以解决问题。 -
@YangYushi 工作如神,感谢急需帮助的兄弟。你能解释一下为什么它应该是双倍的吗?我以为双数是 0.01、0.20、3.5?