好吧,在为此工作了几个小时之后,我认为我想出了一些对于真正的“真正的”程序员来说不那么令人畏缩的东西。
允许我提交我的卑微和可能很糟糕的代码。
它完全有效,但现在我的问题是,我试图让它回到最初的问题,如果响应是负数。我明白了,“嘿!不要输入一个负数!”,然后它会进入下一个提示。这是我目前输出的负输入:C++中的复合兴趣函数
**欢迎使用消费贷款计算器** 您想借多少钱? $ -100 请输入正确的贷款金额。 你的年度百分比率是多少? %
...而对于正输入:
**欢迎消费者贷款计算器** 多少你想借钱? $ 100 你想借多少钱? $ 100 你想借多少钱? $
我想让它回到“你想借多少钱?”如果用户输入为负值,并且只有在输入为正值时才转到下一个问题。我现在做错了什么?
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
using namespace std;
void get_input (double &principal, double &APR, double mon_pay);
int main()
{
double loan; // principal
double APR; // APR
double mon_pay; //monthly payment
cout << "** Welcome to the Consumer Loan Calculator **"<<endl;
do {
cout << "How much would you like to borrow? $";
cin >>loan;
if (loan < 0)
cout <<"Please enter a positive loan amount.";
}
while (loan > 0);
cout << "What is your annual percentage rate? %";
cin >>APR;
cout << "What is your monthly payment? $";
cin >> mon_pay;
APR = APR/100;
get_input (loan, APR, mon_pay);
}
void get_input (double &principal, double &APR, double mon_pay)
{
double total, add=0; // Total payment
int tpay=1; //Total months of payment
while (principal > 0)
{
add = principal * (APR/100);
principal = ((principal+add) - mon_pay);
tpay++;
}
total = mon_pay + principal;
cout << "Your debt will be paid off after "<< tpay << " months, with a final payment of just $" <<setprecision(3)<<total<<endl;
cout <<"Don't get overwhelmed with debt!"<<std::endl;
}
考虑添加作业标签。 – erip 2015-03-03 03:10:52
我刚刚检查,无法找到家庭作业的标签。感谢您的建议。你碰巧对我的问题有什么建议吗? – 2015-03-03 03:12:55
请参阅我的回答以获取建议。 – erip 2015-03-03 03:48:12