返回后浮动变量重置为0.00

时间:2016-02-21 20:05:38

标签: c return

我试图完成我的项目并遇到计算payment的问题。在addCustomer()函数中,我调用了calcMonthlyPayment()函数。在calcMonthlyPayment()函数中,当我打印payment变量时,它会显示正确的答案。但在返回并在payment函数中打印addCustomer()变量后,它将打印为0.00。我不确定我是否遗漏了一些简单的东西?谢谢你的建议!

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "person.h"

// Program Contstants
#define INTEREST    .03
#define CHAR_LEN    40
#define MAX_CUST    100
#define MIN_PRIN    1000
#define MAX_PRIN    1000000
#define MIN_TERM    5
#define MAX_TERM    30

// Program Variables
struct person *customer = NULL;
float payment;

// Function Prototypes
void addCustomer();
struct person *findCustomer(int custID);
void printCustomer();
void flushLine();
float calcMonthlyPayment(float, int);

int main(void)
{
    char input;

    for(;;)
    {
        printf("\n\nChoose From the Options Below:\n");
        printf("Type 'N' to Add a New Customer\n");
        printf("Type 'P' to Print a Customer's Information\n");
        printf("Type 'Q' to Quit the Program\n\n");
        scanf(" %c", &input);
        flushLine();

        switch(toupper(input))
        {
            case 'N':
                addCustomer();
                break;
            case 'P':
                printCustomer();
                break;
            case 'Q':
                exit(0);
            default:
                printf("Invalid Entry. Please Reenter.\n");
                break;
        }
    }
}

void addCustomer()
{
    struct person *cur, *prev, *new_node;

    new_node = malloc(sizeof(struct person));

    if(new_node == NULL)
    {
        printf("The Database is Full. You Cannot Add a New Customer.");
        return;
    }

    printf("\nEnter the Customer ID: ");
    scanf("%d", &new_node->custID);
    flushLine();

    while((new_node->custID <= 0) || (new_node->custID > MAX_CUST))
    {
        printf("Invalid Entry. The Customer ID must be Numeric and Below %d.\n", MAX_CUST);
        printf("\nEnter the Customer ID: ");
        scanf("%d", &new_node->custID);
        flushLine();
    }

    for(cur = customer, prev = NULL; cur != NULL && new_node->custID > cur->custID;     prev = cur, cur = cur->next)
    {
        continue;
    }   

    if(cur != NULL && new_node->custID == cur->custID)
    {
        printf("This Customer ID Already Exists\n");
        free(new_node);
        return;
    }

    printf("\nEnter Customer's First Name: ");
    scanf("%40s", new_node->fName);
    flushLine();

    printf("\nEnter Customer's Last Name: ");
    scanf("%40s", new_node->lName);
    flushLine();

    printf("\nEnter Customer's Street Address: ");
    scanf("%40s", new_node->address);
    flushLine();

    printf("\nEnter Customer's City: ");
    scanf("%40s", new_node->city);
    flushLine();

    printf("\nEnter Customer's 2-Digit State: ");
    scanf("%2s", new_node->state);
    flushLine();

    printf("\nEnter Customer's 5-Digit Zip Code: ");
    scanf("%5s", new_node->zip);
    flushLine();

    printf("\nEnter the Customer's Principal: ");
    scanf("%f", &new_node->principal);
    flushLine();

    while((new_node->principal < MIN_PRIN) || (new_node->principal > MAX_PRIN))
    {
        printf("Invalid Entry. The Customer's Principal must be Between %d and %d.\n", MIN_PRIN, MAX_PRIN);
        printf("\nEnter Customer's Principal: ");
        scanf("%f", &new_node->principal);
        flushLine();
    }

    printf("\nEnter the Customer's Loan Term (In Years): ");
    scanf("%d", &new_node->yearlyTerm);
    flushLine();

    while((new_node->yearlyTerm < MIN_TERM) || (new_node->yearlyTerm > MAX_TERM))
    {
        printf("Invalid Entry. The Loan Term must be Between %d and %d.\n", MIN_TERM, MAX_TERM);
        printf("\nEnter the Customer's Loan Term (In Years): ");
        scanf("%d", &new_node->yearlyTerm);
        flushLine();
    }

    calcMonthlyPayment(new_node->principal, new_node->yearlyTerm);
    printf("Payment Test in Adding: $%.2f", payment);
    new_node->monthlyPayment = payment;


    //calcActualPayment();
    //new_node->actualPayment = ap;

    new_node->next = cur;
    if (prev == NULL)
    {
        customer = new_node;
    }

    else
    {
        prev->next = new_node;
    }
}

struct person *findCustomer(int custID)
{
    struct person *p;

    for(p = customer; p != NULL && custID > p->custID; p = p->next)
    {
        continue;
    }

    if (p != NULL && custID == p->custID)
    {
        return p;
    }

    else
    {
        return NULL;
    }
}

void printCustomer()
{
    int custID;
    struct person *p;

    printf("Enter Customer ID: ");
    scanf("%d", &custID);
    flushLine();

    p = findCustomer(custID);

    if(p != NULL)
    {
        printf("\nCustomer ID:\t\t%d", p->custID);
        printf("\nCustomer's Name:\t%s %s", p->fName, p->lName);
        printf("\nCustomer's Address:\t%s", p->address);
        printf("\n\t\t\t%s, %s %s", p->city, p->state, p->zip);
        printf("\nCustomer's Principal:\t$%.2f", p->principal);
        printf("\nCustomer's Loan Term:\t%d", p->yearlyTerm);
        printf("\nMonthly Payment:\t%.2f", p->monthlyPayment);
    }

    else
    {
        printf("The Customer ID Wasn't Found.\n");
    }
}

float calcMonthlyPayment(float principal, int yearlyTerm)
{
    int monthlyTerm = yearlyTerm * 12;
    float monthlyIR = INTEREST / 12;
    float payment = principal * monthlyIR * (pow(1 + monthlyIR, monthlyTerm) / (pow(1 + monthlyIR, monthlyTerm)-1));

    printf("\nPayment Test in Calculating: $%.2f\n", payment);

    return payment;
}

void flushLine() 
{
    int c;

    while((c = getchar()) != EOF && c != '\n')
    {
        continue;
    }
}

person.h文件:

// Program Contstants
#define CHAR_LEN    40

struct person 
{
    int custID;
    char fName[CHAR_LEN + 1];
    char lName[CHAR_LEN + 1];
    char address[CHAR_LEN + 1];
    char city[CHAR_LEN + 1];
    char state[3];
    char zip[6];
    float principal;
    int yearlyTerm; 
    float monthlyPayment;
    float actualPayment;    
    struct person *next;
};

1 个答案:

答案 0 :(得分:2)

calcMonthlyPayment()函数声明一个名为payment的局部变量,隐藏具有相同名称的全局变量。无论如何,全局变量都是一个坏主意,最好将编写调用函数的代码编写为:

payment = calcMonthlyPayment(new_node->principal, new_node->yearlyTerm);