2015-02-17 109 views
0

我是計算機專業的,我寫我的第一個代碼。代碼是計算汽車的每月支付。雖然我知道我還沒有完成,但我想看看爲什麼我沒有正確地傳遞函數。任何幫助將非常有義務!太少參數功能

#include <iostream> 
#include <string> 
#include <math.h> 

using namespace std; 


void instructions() 
{ 
    cout << "We will calculate the monthly payment for your particular car." << endl; 
    cout << "Please Follow the instructions." << endl; 
} 



string getCarType() 
{ 
    string carType; 

    cout << "Please enter the type of your car: " << endl; 
    cin >> carType; 

    return carType; 
} 




double getPurchasePrice() 
{ 
    double purchasePrice; 

    cout << "Please enter the price in which you purchased the vehicle: " << endl; 
    cin >> purchasePrice; 

    return purchasePrice; 
} 




double getDownPayment() 
{ 
    double downPayment; 

    cout << "Please enter the down payment made on the vehicle: " << endl; 
    cin >> downPayment; 

    return downPayment; 
} 




int getYears() 
{ 
    int years; 

    cout << "Please enter the number of years remaining to pay off the loan: " << endl; 
    cin >> years; 

    return years; 
} 





double getRate() 
{ 
    double rate; 
    double correctedRate; 

    cout << "Please enter the annual interest rate of the loan as a whole number: " << endl; 
    cin >> rate; 

    //calculations 
    correctedRate = (rate/100); 


    return correctedRate; 
} 





double findAmountFinanced(double &purchasePrice, double &downPayment) 
{ 
    double amountFinanced; 

    //calculations 
    amountFinanced = purchasePrice - downPayment; 

    return amountFinanced; 
} 





double findMonthlyPayment(double &amountFinanced, double &rate, int &years) 
{ 
    double monthlyPayment; 
    double sideOne; 
    double sideTwo; 

    //calculations 
    sideOne = amountFinanced * (rate/12); 
    sideTwo = pow(1 - (1 + (rate/12))/(-12*years)); 
    monthlyPayment = sideOne/sideTwo; 


    return monthlyPayment; 
} 




int findNumberOfPayments(int &years) 
{ 
    int payments; 
    payments = 12 * years; 

    return payments; 
} 


int main() 
{ 
    instructions(); 

    string carType; 
    double purchasePrice; 
    double downPayment; 
    int years; 
    double rate; 
    double amountFinanced; 
    double monthlyPayment; 
    int payments; 

    carType = getCarType(); 
    purchasePrice = getPurchasePrice(); 
    downPayment = getDownPayment(); 
    years = getYears(); 
    rate = getRate(); 
    monthlyPayment = findMonthlyPayment(); 
    payments = findNumberOfPayments(); 

    cout << "Make of car: " << carType << endl; 
    cout << "Price Purchased at: " << purchasePrice << endl; 
    cout << "Down payment made at purchase: " << downPayment << endl; 
    cout << "Years to pay off loan: " << years << endl; 
    cout << "Annual rate of interest: " << rate << endl; 
    cout << "Your monthly payment is: " << monthlyPayment << endl; 
    cout << "The total amount of payments is: " << payments << endl; 
    return 0; 
} 

同樣,我的錯誤是,我有太少的參數功能。謝謝!!

+0

Ex;聲明:'findMonthlyPayment(雙&amountFinanced,雙&率,INT和年)',被稱爲'findMonthlyPayment()',結果是編譯器抱怨你沒有足夠的傳遞參數,你不是。清洗,沖洗,重複其他呼叫。那怎麼樣是不可理解的? – WhozCraig 2015-02-18 00:06:15

回答

1

的某些函數中findMonthlyPayment您不會從main傳遞參數,而這些函數需要參數。你的錯誤是不言自明的,你應該自己調試它。

0

如果你看一下findAmountFinanced,findMonthlyPayment和findNumberOfPayments你的方法定義,他們採取的參數被調用時。在你調用它們的main()函數中,你沒有傳遞任何參數。因此,參數太少:)

僅供參考,一招解決該問題是看錯誤信息的完整堆棧跟蹤,並通過行號你的工作方式了。

0

是的,當你調用一個特定的方法和方法具有的參數或參數,只要你撥打你的程序的功能,你需要相同數量的參數和參數類型稱之爲X號。 Ex;