Reputation: 163
I'm trying to finish my project and am running into a problem calculating the payment
. In the addCustomer()
function I call the calcMonthlyPayment()
function. Within the calcMonthlyPayment()
function, when I print the payment
variable, it displays the correct answer. But after returning it and printing the payment
variable in the addCustomer()
function, it prints as 0.00. I'm not sure if I'm missing something simple? Thanks for any advice!
#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 File:
// 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;
};
Upvotes: 0
Views: 123
Reputation: 111259
The calcMonthlyPayment()
function declares a local variable called payment
, hiding the global variable with the same name. Global variables are a bad idea anyway, better to write the code that calls the function as:
payment = calcMonthlyPayment(new_node->principal, new_node->yearlyTerm);
Upvotes: 2