我很擅长编写函数。这是我第一次尝试使用引用参数调用专门用于家庭作业。该计划应根据用户输入的借款金额,每月还款额和利率来计算偿还贷款所需的时间。该程序应确保用户不输入少于每月利息金额的付款编号,并检查负整数等。
我注释掉的区域是我第一次尝试之前我尝试将所有内容转换为使用引用参数调用的函数。那时节目运行正常,我得到的输出在我想要的一美元内。在我重写了这样的程序并试图使它成为一个函数之后,程序编译但在询问利率之后突破到底部的cout语句。我假设我的问题在interest1函数中的某处,我将百分比转换为小数但我无法弄清楚为什么它会跳过其余的函数。
感谢您就此问题提出的任何建议或其他任何与程序本身有关的错误。
#include <iostream>
#include <fstream>
using namespace std;
double amount();
double interest1();
double pay(double amt, double interest);
void payoff(double monthly, double Minpayment,double borrow,double interest, double&
totalinterest,int& month);
int main()
{
double monthly,month,totalinterest;
cout << fixed;
cout <<showpoint;
cout <<setprecision(2);
double borrow=amount();
double interest=interest1();
double Minpayment=pay(borrow, interest);
void payoff(double monthly,double Minpayment,double borrow,double interest,double&
totalinterest,int& month);
/*
// cout << "What is the monthly payment amount? \n";
// cin >> monthly;
// if(monthly<Minpayment)
// {
// cout << "You must make payments of at least: " << Minpayment++ << endl;
// cout << "Because your monthly payment is " << Minpayment << endl;
// cout << "Dont get overwhelmed with debt!!!!!! \n";
// }
// else
//
// {
// int month = 1;
// double totalinterest=0;
// do
// {
// double Minpayment=pay(borrow, interest);
// borrow=(borrow+Minpayment)-monthly;
// totalinterest=totalinterest+Minpayment;
// month++;
// }while(borrow>monthly);
*/
cout << "Your debt will be paid off in " << month << " months \n";
cout <<" with a final payment of " << borrow << endl;
cout << "You paid " << totalinterest << " in interest. \n";
return 0;
}
double amount()
{
double borrow,amt;
do
{
cout << "How much money do you want to borrow? \n";
cin >> amt;
if(amt<1)
cout << "You must enter a positive number!";
}while(amt<1);
borrow = amt;
return borrow;
}
double interest1()
{
double rate;
do
{
cout << "What is the annual interest rate expressed as a percent? \n";
cin >> rate;
if(rate<1)
cout << "You must enter a positive number!";
}while(rate<1);
double interest = (rate/12)*.01;
return interest;
}
double pay(double borrow,double interest)
{
double Minpayment=borrow*interest;
return Minpayment;
}
void payoff(double monthly,double Minpayment,double borrow,double interest, double&
totalinterest,int& month)
{
cout << "What is the monthly payment amount? \n";
cin >> monthly;
if(monthly<Minpayment)
{
cout << "You must make payments of at least: " << Minpayment++ << endl;
cout << "Because your monthly payment is " << Minpayment << endl;
cout << "Dont get overwhelmed with debt!!!!!! \n";
}
else
{
int month = 1;
do
{
double Minpayment=pay(borrow, interest);
borrow=(borrow+Minpayment)-monthly;
double totalinterest=totalinterest+Minpayment;
month++;
}while(borrow>monthly);
}
}
答案 0 :(得分:1)
当您在main
内调用函数时,您不再需要指定参数类型:
void payoff(double monthly,double Minpayment,double borrow,double interest,double&
totalinterest,int& month); //wrong
应该是
payoff(monthly, Minpayment,borrow, interest, totalinterest, month);
在定义函数时,应将double&
和int&
放入函数的参数列表中。