好吧,在爲此工作了幾個小時之後,我認爲我想出了一些對於真正的「真正的」程序員來說不那麼令人畏縮的東西。
允許我提交我的卑微和可能很糟糕的代碼。
它完全有效,但現在我的問題是,我試圖讓它回到最初的問題,如果響應是負數。我明白了,「嘿!不要輸入一個負數!」,然後它會進入下一個提示。這是我目前輸出的負輸入: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