having trouble with functions
Hey everyone,
I am having trouble with writing a program that uses functions. I am very new to programming and am having a hard time grasping some concepts.
What I need to do is write a series of functions to prompt the user for some input, then I need to return that input to the main then use it in another function to do some calculations then return the calculations back to the main and print the results in another function.
I can't get my spacing to format in the code tags but hopefully it is still readable.
Also I know its incomplete I had it working while using integers but I need to have 2 decimals for my numbers so I deleted some things to go back and try and debug.
Any help would be appreciated
Code:
#include <stdio.h>
int accno();
double balance();
double purchase();
double payment();
double credit();
double iendbal();
double accint();
double fendbal();
printfun();
int main ()//main function
{
//gather information from functions
int acc = 0;
double bal;
double pur;
double pay;
double cr;
double interest;
double interim;
double final;
int tcust;
double totfeb;
static counter = 1;
acc = accno();
bal = balance();
pur = purchase();
pay = payment();
cr = credit();
interim = iendbal(&bal, &pur, &pay, &cr);
interest = accint(&interim);
final = fendbal(&interest, &interim);
printfun(&acc,&bal,&pur,&pay,&cr,&interest,&final);
return 0;
}
int accno()
{
int x;
printf("Account Number(between 1000 and 4000 only) - Enter -9999 to Quit: ");
scanf("%d", &x);
if(x >= 1000 && x <= 4000)
return (x);
else if( x== 9999)
return test();
else
return printf("Invalid Account Number. Please Try Again\n\n"); accno();
}
double balance ()
{
double x;
printf("Beginning Balance: ");
scanf("%lf", &x);
return (x);
}
double purchase()
{
double x;
printf("Purchases: ");
scanf("%lf", &x);
if(x >= 0)
return (x);
else
return printf("Invalid Entry. Please Try Again\n\n"); purchase();
}
double payment()
{
double x;
printf("Payments: ");
scanf("%lf", &x);
if(x >= 0)
return (x);
else
return printf("Invalid Entry. Please Try Again\n\n"); payment();
}
double credit ()
{
double x;
printf("Credits/Returns: ");
scanf("%lf", &x);
return (x);
}
double iendbal(x, y, z, a)
{
return x + y - z - a;
}
double accint(x)
{
return x * .14;
}
double fendbal(x,y)
{
return x + y ;
}
int test ()
{
static counter;
printf("Total Number of Entries: %d\n", counter);
return 0;
}
printfun(x, y, z, a, b, c, d)
{
printf("\nAccount Number: %d\n", x);
printf("Beginning Balance: %.2f\n", y);
printf("Purchses: %.2f\n", z);
printf("Payments: %.2f\n", a);
printf("Credits: %.2f\n", b);
printf("Interest Accrued: %.2f\n", c);
printf("Final Ending Balance: %.2f\n\n", d);
return 0;
}
}
double purchase()
{
double x;
printf("Purchases: ");
scanf("%lf", &x);
if(x >= 0)
return (x);
else
return printf("Invalid Entry. Please Try Again\n\n"); purchase();
}
double payment()
{
double x;
printf("Payments: ");
scanf("%lf", &x);
if(x >= 0)
return (x);
else
return printf("Invalid Entry. Please Try Again\n\n"); payment();
}
double credit ()
{
double x;
printf("Credits/Returns: ");
scanf("%lf", &x);
return (x);
}
double iendbal(x, y, z, a)
{
return x + y - z - a;
}
double accint(x)
{
return x * .14;
}
double fendbal(x,y)
{
return x + y ;
}
int test ()
{
static counter;
printf("Total Number of Entries: %d\n", counter);
return 0;
}
printfun(x, y, z, a, b, c, d)
{
printf("\nAccount Number: %d\n", x);
printf("Beginning Balance: %.2f\n", y);
printf("Purchses: %.2f\n", z);
printf("Payments: %.2f\n", a);
printf("Credits: %.2f\n", b);
printf("Interest Accrued: %.2f\n", c);
printf("Final Ending Balance: %.2f\n\n", d);
return 0;
}