int main(int argc, _TCHAR* argv[])
{
double amount;
double rate;
int numPayments;
double payment = (rate * pow((1 + rate), numPayments) / (pow((1+rate),numPayments)-1)) * amount;
cout << "Loan amount: " << endl;
cin >> amount;
cout << "Monthly interest rate (.12 for 12%): " << endl;
cin >> rate;
cout << "Number of monthly payments to be made: " << endl;
cin >> numPayments;
cout << "Loan amount: " << amount << endl;
cout << "Monthly interest rate: " << rate << endl;
cout << "Number of Payments: " << numPayments << endl;
cout << "Monthly payment: " << (amount/numPayments)*rate;
cout << "Amount paid back: " << (amount*rate)+amount;
cout << "Interest paid: " << amount*rate;
}
嗨,我正在尝试编写一个相当简单的兴趣计算程序...继续使用'初始化的局部变量'。我对C ++很新,所以我确信它很简单......
答案 0 :(得分:1)
我不确定问题究竟是什么......但我认为这不会起作用。
double amount;
double rate;
int numPayments;
double payment = (rate * pow((1 + rate), numPayments) / (pow((1+rate),numPayments)-1)) * amount;
因为您没有使用值初始化它们,所以它们将具有随机值。这就是为什么不应该在rate
中使用payment
。
我建议用0初始化它们。
double amount = 0.0;
double rate = 0.0;
int numPayments = 0;
cout << "Loan amount: " << endl;
cin >> amount;
cout << "Monthly interest rate (.12 for 12%): " << endl;
cin >> rate;
cout << "Number of monthly payments to be made: " << endl;
cin >> numPayments;
double payment = (rate * pow((1 + rate), numPayments) / pow((1+rate),numPayments)-1)) * amount;
//Now you can print
答案 1 :(得分:1)
如Mike所述,您需要在使用变量之前分配(“给出值”)变量。 您可以在获取输入数据后移动付款分配,如下所示:
double amount;
double rate;
int numPayments;
double payment;
cout << "Loan amount: " << endl;
cin >> amount;
cout << "Monthly interest rate (.12 for 12%): " << endl;
cin >> rate;
cout << "Number of monthly payments to be made: " << endl;
cin >> numPayments;
payment = (rate * pow((1 + rate), numPayments) / (pow((1+rate),numPayments)-1)) * amount;
答案 2 :(得分:0)
您编码的方式是在用户输入金额,费率,numPayments的值之前计算付款。
这一行
double payment = (rate * pow((1 + rate), numPayments) / (pow((1+rate),numPayments)-1)) * amount;
需要在之后执行,以便从用户那里读取变量,即
cout << "Loan amount: " << endl;
cin >> amount;