我的代码中的函数不起作用

时间:2016-10-03 01:07:31

标签: c codeblocks

我尝试运行该程序,但它运行不正常。问题在于功能,但我不确切知道在哪里。我首先声明了函数,然后我尝试在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);

}

3 个答案:

答案 0 :(得分:0)

main()中查看第48..50行。

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;
}

答案 1 :(得分: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()中编写更多代码以使用这些变量。

答案 2 :(得分:0)

请记住其他答案所说的有关正确调用您的功能的内容。下面的代码应该有效。我不得不重新计算你的computeMonthlyPayment,因为它计算错误。

<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type igDP:CellValuePresenter}}, Path=Record.DataItem.Product}" />
                                            <Setter Property="DisplayMemberPath" Value="Value" />