2016-10-03 3 views
-3

프로그램을 실행하려했지만 올바르게 실행되지 않았습니다. 문제는 함수에서 발생하지만 정확한 위치를 모른다. 먼저 함수를 선언 한 다음 주 함수로 호출하려고했습니다. 그러나, 나는 그것이 사실인지 모른다. 문제는 함수 정의에 있다고 생각합니까? 그러나 나는 그 밖에 할 일이 없다. 누구든지 그것을보고 나에게 그것을 지적하면 좋을 것입니다.내 코드의 기능이 작동하지 않습니다.

#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()에 함수를 다시 선언하고 실제로 호출하지 않습니다. 이런 식으로 뭔가를 시도 : n가 초기화되지 않기 때문에

int main() 
{ 

    // ... 

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

    // ... 
} 

이 여전히하지만, 100 % 고정입니다. 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 

    }