2016-10-03 21 views
-3

我試着運行該程序,但它運行不正常。問題在於功能,但我不知道確切的位置。我首先聲明瞭這些函數,然後我嘗試在main函數中調用它們。但是,我不確定是這種情況。我認爲問題在於函數定義?但我不知道還有什麼可以做的。如果任何人都能看到它,並向我指出這將是偉大的。我的代碼中的功能不起作用

#include <stdio.h> 
#include <math.h> 

float computeMonthlyPayment(float p, float YearRate, float YearTerm); 
float computeMonthlyInterest(float p, float YearRate); 
void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n); 
void printTable(float MonthlyPayment, float p, float YearRate,float YearTerm); 

int main() 
{ 

    float n, p, r, MonthlyPayment, YearRate, YearTerm; 

    printf("Enter the loan amount: "); 
    scanf("%f", &p); 

    if (p <= 0.0) 
     printf("\nERROR: Invalid rate; must be greater than 0\n"); 
     while (p <= 0.0) 
     { 
      printf("\nEnter the loan amount: "); 
      scanf("%f", &p); 
     } 

    printf("\nEnter annual interest rate: "); 
    scanf("%f", &YearRate); 

    if (YearRate <= 0.0 || YearRate > 30.0) 
     printf("\nERROR: Invalid rate; must be > 0.0 amd <= 30.0.\n"); 
     while (YearRate <= 0.0 || YearRate > 30.0) 
     { 
      printf("\nEnter annual interest rate: "); 
      scanf("%f", &YearRate); 
     } 

    printf("\nEnter the term of the loan (in years): "); 
    scanf("%f", &YearTerm); 

    if (YearTerm <= 0.0) 
     printf("\nERROR: Invalid rate; must be greater than 0\n"); 
     while (YearTerm <= 0.0) 
     { 
      printf("\nEnter the term of the loan (in years): "); 
      scanf("%f", &YearTerm); 
     } 


    float computeMonthlyInterest(float p, float YearRate); 
    float computeMonthlyPayment(float p, float YearRate, float YearTerm); 
    void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n); 


    return 0; 
} 


float computeMonthlyPayment(float p, float YearRate, float YearTerm) 
{ 
    float r = YearRate/12; 
    float n = YearTerm*12; 
    float MonthlyPayment = 0; 

    MonthlyPayment = (r*p)/1-((1+r)/n); 

    return MonthlyPayment; 
} 


float computeMonthlyInterest(float p, float YearRate) 
{ 
    float r = 0; 
    r = ((YearRate)/12)/12; 

    return r; 

} 


void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n) 
{ 

    printf("LOAN INFORMATION\n"); 
    printf("-----------------------\n"); 
    printf("Initial loan amount: %.2f\n", p); 
    printf("Annual interest rate: %.3f\n", YearRate); 
    printf("Monthly interest rate: %.3f\n", r); 
    printf("Term of loan (years): %f\n", YearTerm); 
    printf("Term of loan (months): %f\n", n); 
    printf("Monthly payment amount: %.2f\n", MonthlyPayment); 

} 
+4

你需要比這個更具體* * *。說'問題出在功能上'有點像飛機機械師說的'問題出在飛機的某個地方'。當你說你的程序不起作用你是什麼意思?它編譯失敗嗎?如果是這樣,您需要提供錯誤的詳細信息。如果不是,則需要提供您正在提供的輸入的詳細信息和您期望的輸出以及實際輸出。更好的是,提供一個MVCE(http://stackoverflow.com/help/mcve) –

+0

將來,請解釋你的程序的預期行爲是什麼以及它實際做了什麼;如果編譯失敗,那麼告訴我們你的編譯器給你什麼錯誤消息。儘管一旦你開始尋找它,問題就突顯出來,但情況並非總是如此。即使其他人在嘗試運行代碼時遇到問題,他們也無法確定是否遇到了同樣的問題。謝謝。 –

回答

0

看看第48..50行,在main()

int main() 
{ 

    // ... 

    float computeMonthlyInterest(float p, float YearRate); 
    float computeMonthlyPayment(float p, float YearRate, float YearTerm); 
    void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n); 

    // ... 
} 

這些是函數的原型,而不是你實際調用它們的方式。您應該按照您撥打printf()scanf()的方式給他們打電話,方法是向他們傳遞適當的參數。就目前而言,您只需在main()中重新聲明函數,而不是實際調用它們。嘗試是這樣的:

int main() 
{ 

    // ... 

    r = computeMonthlyInterest(p, YearRate); // r should be labeled better. 
    MonthlyPayment = computeMonthlyPayment(p, YearRate, YearTerm); 
    printLoanInfo(p, MonthlyPayment, YearRate, r, YearTerm, n); 

    // ... 
} 

這仍然不是100%固定的,但是,因爲n沒有被初始化。雖然printLoanInfo()說明它是什麼(這也是我如何識別r),但您忘記將其設置在main()。所以..

int main() 
{ 

    // ... 

    r = computeMonthlyInterest(p, YearRate); 
    n = YearTerm * 12; // You did this in computeMonthlyPayment(), but not here. 
    MonthlyPayment = computeMonthlyPayment(p, YearRate, YearTerm); 
    printLoanInfo(p, MonthlyPayment, YearRate, r, YearTerm, n); 

    // ... 
} 

因此,在考慮到這些變化,你main()或許應該是這樣的:

int main() 
{ 

    float n, p, r, MonthlyPayment, YearRate, YearTerm; 

    printf("Enter the loan amount: "); 
    scanf("%f", &p); 

    if (p <= 0.0) 
     printf("\nERROR: Invalid rate; must be greater than 0\n"); 
     while (p <= 0.0) 
     { 
      printf("\nEnter the loan amount: "); 
      scanf("%f", &p); 
     } 

    printf("\nEnter annual interest rate: "); 
    scanf("%f", &YearRate); 

    if (YearRate <= 0.0 || YearRate > 30.0) 
     printf("\nERROR: Invalid rate; must be > 0.0 amd <= 30.0.\n"); 
     while (YearRate <= 0.0 || YearRate > 30.0) 
     { 
      printf("\nEnter annual interest rate: "); 
      scanf("%f", &YearRate); 
     } 

    printf("\nEnter the term of the loan (in years): "); 
    scanf("%f", &YearTerm); 

    if (YearTerm <= 0.0) 
     printf("\nERROR: Invalid rate; must be greater than 0\n"); 
     while (YearTerm <= 0.0) 
     { 
      printf("\nEnter the term of the loan (in years): "); 
      scanf("%f", &YearTerm); 
     } 


    // Remove these lines: 
    // float computeMonthlyInterest(float p, float YearRate); 
    // float computeMonthlyPayment(float p, float YearRate, float YearTerm); 
    // void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n); 


    // Add these lines: 
    r = computeMonthlyInterest(p, YearRate); 
    n = YearTerm * 12; 
    MonthlyPayment = computeMonthlyPayment(p, YearRate, YearTerm); 
    printLoanInfo(p, MonthlyPayment, YearRate, r, YearTerm, n); 


    return 0; 
} 
0

的問題不在於功能的定義。這是事實,在main(),你有複製並粘貼這些行。

float computeMonthlyInterest(float p, float YearRate); 
float computeMonthlyPayment(float p, float YearRate, float YearTerm); 
void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n); 

這些函數聲明通知編譯器如何調用函數,但不實際調用它們。

如果要真正給他們打電話,你需要做的是這樣

variable1 = computeMonthlyInterest(p, YearRate); 
variable2 = computeMonthlyPayment(p, YearRate, YearTerm); 
printLoanInfo(p, MonthlyPayment, YearRate, r, YearTerm, n); 

本質上講,這將刪除函數參數類型說明並通過實際值。 variable1variable2是您需要在這些語句之前定義(可能包含更多有意義的名稱)以保存返回值的變量。據推測,您還需要在main()中編寫更多代碼才能使用這些變量。

0

記住其他答案所說的有關正確調用您的函數的內容。下面的代碼應該可以工作。我不得不重新計算您的computeMonthlyPayment,因爲它計算錯誤。

#include <stdio.h> 
    #include <math.h> 

    float computeMonthlyPayment(float p, float YearRate, float YearTerm); 
    float computeMonthlyInterest(float p, float YearRate); 
    void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n); 
    void printTable(float MonthlyPayment, float p, float YearRate,float YearTerm);//why are you defining this if it doesn't exist? 

    int main() 
    { 

     float n=0, p, r=0, MonthlyPayment=0, YearRate, YearTerm; 

     printf("Enter the loan amount: "); 
     scanf("%f", &p); 

     if (p <= 0.0) 
      printf("\nERROR: Invalid rate; must be greater than 0\n"); 
      while (p <= 0.0) 
      { 
       printf("\nEnter the loan amount: "); 
       scanf("%f", &p); 
      } 

     printf("\nEnter annual interest rate: "); 
     scanf("%f", &YearRate); 

     if (YearRate <= 0.0 || YearRate > 30.0) 
      printf("\nERROR: Invalid rate; must be > 0.0 amd <= 30.0.\n"); 
      while (YearRate <= 0.0 || YearRate > 30.0) 
      { 
       printf("\nEnter annual interest rate: "); 
       scanf("%f", &YearRate); 
      } 

     printf("\nEnter the term of the loan (in years): "); 
     scanf("%f", &YearTerm); 

     if (YearTerm <= 0.0) 
      printf("\nERROR: Invalid rate; must be greater than 0\n"); 
      while (YearTerm <= 0.0) 
      { 
       printf("\nEnter the term of the loan (in years): "); 
       scanf("%f", &YearTerm); 
      } 

     printLoanInfo(p,MonthlyPayment,YearRate,r,YearTerm,n); 

     } 


    float computeMonthlyPayment(float p, float YearRate, float YearTerm) 
    { 
     float r = YearRate/(12*100); 
     float n = YearTerm*12; 

     return (p*r*pow(1 + r, n))/(pow(1 + r, n) - 1); // just return the calculation  
    } 


    float computeMonthlyInterest(float p, float YearRate) 
    { 
     float r = 0; 

     return (r = ((YearRate)/12)/12); 
    } 


    void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n) 
    { 

     printf("LOAN INFORMATION\n"); 
     printf("-----------------------\n"); 
     printf("Initial loan amount: %.2f\n", p); 
     printf("Annual interest rate: %.3f\n", YearRate); 
     printf("Monthly interest rate: %.3f\n",  computeMonthlyInterest(p,YearRate)); //call function computeMonthlyInterest 
     printf("Term of loan (years): %.0f\n", YearTerm); 
     printf("Term of loan (months): %.0f\n", (YearTerm * 12));//simple calc for number of months 
     printf("Monthly payment amount: %.2f\n", computeMonthlyPayment(p,YearRate,YearTerm)); // call function computeMonthlyPayment 

    }