• 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?
 
messiahConnect With a Mentor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.