-4
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++ 很陌生,所以我确信这很简单......

4

3 回答 3

1

我不确定问题到底是什么......但我认为这不会奏效。

double amount;
double rate;
int numPayments;
double payment = (rate * pow((1 + rate), numPayments) /            (pow((1+rate),numPayments)-1)) * amount;

因为您没有用值初始化它们,所以它们将具有随机值。这就是为什么不应该ratepayment这里使用。

我建议用 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
于 2013-09-10T14:40:43.460 回答
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;
于 2013-09-10T14:43:11.530 回答
0

您编码的方式是在用户输入金额、费率、numPayments 值之前计算付款。

这条线

double payment = (rate * pow((1 + rate), numPayments) /            (pow((1+rate),numPayments)-1)) * amount;

需要在从用户读取变量的行之后执行,即;

cout << "Loan amount: " << endl;
cin >> amount;
于 2013-09-10T14:44:01.130 回答