Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

not calculating, not making output file, not correcting

Status
Not open for further replies.

storygeek

Technical User
Jun 25, 2004
5
US
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double calcdown (double n1, double n2);
double calcfinanced (double f1,double f2);
double calcmonthly (double y1);
double in (double g1,double g2);
void write3val (double h1, double h2, double h3, double h4, double h5, double h6, double h7, double h8);
main()
{
int ans, id;
double perin, downpay, monthly, perdown,interate, finan, hou;
printf ("Enter 1 to run program; enter any other key to stop. Key = ");
scanf ("%d", &ans);
while (ans == 1)
{
printf ("\nId? ");
scanf ("%d", &id); /*if %d doens't work try %.2f*/
printf ("\nPrice of the house = "); /*remember to check the price of the house 'n' percent interest*/
scanf ("%d", &hou);
printf ("\nChoose current interest rate: 6.0, 7.0, 8.0, 9.0, 10.0, 10.5, 11.0. =% "); /* if this % fails use escape sequence*/
scanf ("%d", &perin);
while((perin < 6) || (perin > 11))
{
printf ("You typed an invalid value. Choose current interest rate: 6.0, 7.0, 8.0, 9.0, 10.0, 10.5, 11.0. =% ");
scanf ("%d", &perin); /* remember to change here if %d doesn't work*/
}
printf ("\nPercent as down payment = % ");
scanf ("%d", &perdown);
downpay = calcdown (perdown, hou);
finan = calcfinanced (hou, downpay);
monthly = calcmonthly (perin); /* mark */
interate = in (monthly, hou);
write3val (downpay, finan, monthly, interate, id, hou, perin, perdown);
printf ("\n\nId = %d", id);
printf ("\nHouse price = %d", hou);
printf ("\nInterest rate = %d", perin);
printf ("\nPercent downpayment = %d", perdown);
printf ("\n\nDown Payment = %d", downpay);
printf ("\nAmount Financed = %d", finan);
printf ("\nMonthly pay = %d", monthly);
printf ("\nTotal Interest = %d", interate);
printf ("\n\nEnter 1 to run program; enter any other key to stop. Key = ");
scanf ("%d", &ans);
}
}
double calcdown (double n1, double n2)
{
double p;
p = (n1 *.01) * n2;
return (p);
}
double calcfinanced (double f1,double f2)
{
double h;
h = f1 - f2;
return (h);
}
double calcmonthly (double y1)
{
double y2;
if (y1 == 6.00)
{
y2 = y1 * (6.00 * .001);
}
else if (y1 == 7.00)
{
y2 = y1 * 6.66 * .001;
}
else if (y1 == 8.00)
{
y2 = y1 * 7.34 * .001;
}
else if (y1 == 9.00)
{
y2 = y1 * 8.05 * .001;
}
else if (y1 == 10.00)
{
y2 = y1 * 8.78 * .001;
}
else if (y1 == 10.50)
{
y2 = y1 * 9.15 * .001;
}
else if (y1 == 11.00)
{
y2 = y1 * 9.52 * .001;
}
return (y2);
}
void write3val(double h1, double h2, double h3, double h4, double h5, double h6, double h7, double h8)
{
FILE *output;
output = fopen ("C:/homeloan.txt", "w");
fprintf (output,"\n\nId = %d", h5);
fprintf (output,"\nHouse price = %d", h6);
fprintf (output,"\nInterest rate = %d", h7);
fprintf (output,"\nPercent downpayment = %d", h8);
fprintf (output,"\nDown Payment = %.d", h1);
fprintf (output,"\nAmount Financed = %.d", h2);
fprintf (output,"\nMonthly pay = %.d", h3);
fprintf (output,"\n\nTotal Interest = %.d", h4); /* remember to put fclose somewhere*/
}
double in (double g1,double g2)
{
double t;
t = (g1 * 12 * 30) - g2;
return (t);
}

This is a program that calculates down payment, amount financed, monthly pay, and total interest for a home loan with functions that calculate for each and and function that does the printing. Problem here is when I called the function void write3val from main the a while loop it doesn't copy the calculations to the output file. I tried puting the file in the parameter of write 3 val; but then when I call it while running the program I get an error that the function calls more than 8 arguments, which is the extra file I put in the parameter. Another problem is that when I run the program it gives me 0 for downpayment, 1000 for amount financed, 0 for monthly pay, and 1000 for total interest; where 1000 came from my input of the price of the house. I variated the calculations but still got problems I check whether I got my functions right also and they seemed alright but still got problems with my calculations. Finally the last problem I checking my interest rate; I'm sure that "while((perin < 6) || (perin > 11))" would do the trick check if the user entered the wrong value that the user would be promted to enter the given values again but it didn't work. Your help is greatly appreciated, Greatly!
 
1. Use this forum TGML tag CODE for your text snippet (see help link Process TGML on this form).
2. Proper main()signatures are
Code:
int main()
or
int main(int arc, char*argv[])
in C. main() returns int (0 on success).
3. Use %lf format specifier to scanf (instead of %d for ints only;) doubles (see C language library help). You have uninitialized values when use %d for doubles.
4. Use %f format specifier to printf doubles.
5. Use %% twice to represent percent char in printf format string.
6. Using \n as a format leading char is error-prone: you may forget to insert carriage return at the last line (and you do that;)...
7. Try pass your output file name via main() parameters (as cmd string parameter). See argc/argv main() parameters in any C book (after pp 1-5 correction;)...

Correct your scanf/printf format strings and try again...
 
Oh, last but not least:
1. Are you ever hear about a proper source text indentation?
2. What for your temp vars in short function bodies? Try:
Code:
double in(double g1, double g2)
{
  return (g1 * 12 * 30) - g2;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top