我对编写函数很陌生。这是我第一次尝试使用专门用于家庭作业的引用参数调用。该程序应根据用户输入的借款金额、每月还款额和利率来计算还清贷款所需的时间。程序应确保用户输入的付款数字不小于每月利息金额并检查负整数等。
我注释掉的区域是我在尝试将所有这些转换为使用引用参数调用的函数之前的第一次尝试。那时程序运行良好,我得到的输出结果只差一美元。在我像这样重写程序并尝试使其成为一个函数后,程序编译但在询问利率后中断到底部 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);
}
}