• C

check my work - spot my errors

would this create an array (called record) of structures reading info into each from the file cartran.dat and putting the stuff read in and some calculations from it into report1.dat.  Also, how would I read it in to the other file report2.e the reading and writing part of a function call and call it for both files - rep1 and rep2????

# include <stdio.h>
# include <math.h>
# define N 25

struct data {     char transcode[10]
                        char cartype[15]
                        int pp;
                        int dp;
            float yir;
                          int mtp;
           };
struct package  {
                struct data entry;
                float monthly payment;
                int amt_borr;
                float total_int;
                         };

           
/*calculates the monthlypayments required by the nature of the loan*/
/*is given x(purchaseprice of the car,z(downpayment made on the car)*/
/*y(the yearly interest rate), and m(the months to pay or duration of the payback period)*/

float carpayment(int x, int z, float y, int m);
double power(int x, int n);


int main()
{
FILE  *fpi, *fpo;
struct data entry;
struct package record[ N];


/*This checks that the files are open*/

if((fpi=fopen(“cartran.dat”, “r”))==NULL)
{printf(“cannot open cartran.dat for reading.\n”);
exit(1);}
if((fpo=fopen(“report1.dat”,”w”))==NULL)
{printf(“cannot open report1.dat for writing”))==NULL)
exit(1);}
if((fpo=fopen(“report2.dat”,”w”))==NULL)
{printf(“cannot open report2.dat for writing”))==NULL)

/*this prints out the headings for the data to be entered*/

fprintf(fpo,”transcode    cartype   pp   dp   amt_borr   yir   mtp   mp  total_int \n”);
fprintf(fpo,”============================================”);


/*This scans the input file and prints the desired info into the output file*/

for(i=0; i<NUMBER_OF_RECORDS;++i)
   while(fscanf(fpi,”%s%s%d%d%f%d”, record[i].entry.transcode,                               record[i].entry.cartype, record[i].entry.pp, record[i].entry.dp,  record[i].entry.yir,              record[i].entry.mtp)!=EOF)
         {
            record[i].monthlypayment=carpayment(record[i].entry.pp, record[i].entry.dp,                record[i].entry.yir, record[i].entry.mtp);
      record[i].amt_borr= record[i].entry.pp- record[i].entry.dp;
      record[i].total_int=(monthlypayment* record[i].entry.mtp)-( amt_borr);
            fprintf(fpo,”%s %s %d %d %d %6.2f %d %6.2f %6.2f”,       record[i].entry.transcode, record[i].entry.cartype, record[i].entry.pp,       record[i].entry.dp, record[i].amt_borr, record[i].entry.yir, record[i].entry.mtp,       record[i].monthlypayment, record[i].total_int);
         }


return (0);
}

/*precondition: recieves purchase price, downpayment, yearly interest rate and months to pay*/
/*postcondition: calculates and returns the monthly payments on the loan*/


car payment ( pp, dp, yir, mtp)   /*function that calculates the monthly payment required.*/      
   {                                               /*...including interest on loan*/
     int n, x, amt_borr;
     float mir, a, b, bracket, monthlypayment;
     
     mir=yir/12/100;
     n=mtp;
     x=mir+1;
     amt_borr=pp-dp;
     bracket=power(x,n)
 
     
    a=n*amt_borr;
    b=bracket/bracket-1;
    return monthlypayment;
  }

/*precondition: recieves two integers x and n*/
/*postcondition: returns x to the nth value*/

power(x,n)            /*recursive function that computes x to the n power*/
  { if(n==0)
        return 1;
     else
        return x* power(x, n-1)







 
fharrisAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

messiahCommented:
Here is a quickie version of your program
that's off the top of my head.. enjoy

// This should work, or give you a clue :)

#include <stdio.h>
#include <math.h>
#define  N 25

typedef struct
{
      char transcode[10];
      char cartype[15];
      int pp;
      int dp;
      float yir;
      int mtp;
} data;
typedef struct
{
      data entry;
      float monthlypayment;
      int amt_borr;
      float total_int;
} package;

float carpayment(int x, int z, float y, int m);
double power(int x, int n);

int main()
{
      FILE *fpi, *fpo, *fpe;
      data entry;
      package record[N];

      /* Save them to binary files.. it's a lot easier */
      fpi = fopen("cartran.dat", "rb");
      fpo = fopen("report1.dat", "wb");
      fpe = fopen("report2.dat", "wb");

      if(fpi == NULL || fpo == NULL || fpe == NULL)
      {
            printf("\nFile I/O Error\n");
            return -1;
      }

      for(int i=0; i < N; i++)
            while(!feof(fpi))
            {
                  fread(&record, sizeof(record), 1, fpi);
                  record[i].monthlypayment=carpayment(record[i].entry.pp, record[i].entry.dp, record[i].entry.yir, record[i].entry.mtp);
                  record[i].amt_borr= record[i].entry.pp- record[i].entry.dp;
                  record[i].total_int=(record[i].monthlypayment* record[i].entry.mtp)-(record[i].amt_borr);
                  fwrite(&record, sizeof(record), 1, fpe);
            }

fcloseall();
return (0);
}

float carpayment(pp,dp,yir,mtp)
{
 int n, x, amt_borr;
 float mir, a, b, bracket, monthlypayment;

 mir=yir/12/100;
 n=mtp;
 x=mir+1;
 amt_borr=pp-dp;
 bracket=power(x,n)

 a=n*record[i].amt_borr;
 b=bracket/bracket-1;
 return monthlypayment;
}

double power(x,n) /*recursive function that computes x to the n power*/
{
      if(n==0) return 1;
      else return x* power(x, n-1);
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.