Could someone please check my code and tell me what I'm doing wrong.
The problem lies in the second function and possibly the second while statement. I am trying to read from the second infile, which needs to be the searchInt array, then search that array and return the locations of some integers in the file. Those integers need to be linked to some scores from the first in file then the two numbers need to be printed in the outfile. It should look something like this.
Just the stuff from the searchInt function:
The first column of numbers are from the infile and are the array, the second column is what needs to be found from the first Infile.
1 7
5 8
I apologize if I didn't explain that very well, so if you have questions let me know. I appreciate any help I can get on this. I am new to this and I just don't quite get it. I will say this is for a class, I have asked for help but I still don't understand it. I would appreciate if someone could tell me how I need to link the variables to the array and then back to the main part of the file. Thanks ahead of time for your advice.
Code:#include <stdio.h>
#include<stdlib.h>
#define MAX 40
//int summaryResults(int [],int []);
void printArrayLF(int [],int [],int,FILE *);
int searchInt(int [],int ,int );
main()
{
int id[MAX];
int score[MAX];
int idNum,j1,j2,j3,totalScore,ctr,total,e,i,idnbr;
float Avg;
FILE *InFile,*inFile, *OutFile;
InFile=fopen("file1.txt","r");
inFile=fopen("file2.txt","r");
OutFile=fopen("outproj5.txt","w");
printf("Candidate Results\n");
printf(" Id J1 J2 J3 score\n");
fprintf(OutFile,"Candidate Results\n");
fprintf(OutFile," Id J1 J2 J3 score\n");
for (i=0;i<MAX;i++)
{
id[i]=0;
score[i]=0;
}
ctr=0;
total=0;
e=fscanf(InFile,"%d %d %d %d",&idNum,&j1,&j2,&j3);
while(e==4)
{
totalScore = j1+j2+j3;
id[ctr]=idNum;
score[ctr]=totalScore;
printf("%3d %6d %6d %6d %9d\n",idNum,j1,j2,j3,totalScore);
fprintf(OutFile,"%3d %6d %6d %6d %9d\n",idNum,j1,j2,j3,totalScore);
ctr=ctr+1;
total = totalScore + total;
e=fscanf(InFile,"%d %d %d %d",&idNum,&j1,&j2,&j3);
}
Avg=1.0*total/ctr;
printf(" %d Total Candidates\n",ctr);
fprintf(OutFile," %d Total Candidates\n",ctr);
printf(" Average Total Score is %.1f \n",Avg);
fprintf(OutFile," Average Total Score is %.1f\n",Avg);
printArrayLF(id,score,ctr,OutFile);
printf("Score Check\n");
fprintf(OutFile,"Score Check\n");
e=fscanf(inFile,"%d",id);
while (e==1)
{
int scoreCheck,idNum;
id[ctr]=idNum;
scoreCheck=searchInt(id,ctr,idnbr);
if (scoreCheck<0)
{
printf("not found\n");
fprintf(OutFile,"not found\n");
}
else
{
scoreCheck=searchInt(id,ctr,idnbr);
printf("%3d %10d\n",idNum,score);
fprintf(OutFile,"%3d %10d\n",idNum,score);
}
e=fscanf(inFile,"%d",id);
}
printf("End of Program\n");
fprintf(OutFile,"End of Program\n");
fclose(OutFile);
fclose(InFile);
system("pause");
}
void printArrayLF(int a[],int b[],int ctr,FILE *fl)
{
int k;
printf("\n Summarized Results\n");
fprintf(fl,"\n Summarized Results\n");
printf(" Id totalScore\n");
fprintf(fl," Id totalScore\n");
for(k=0;k<ctr;k++)
{
printf("%3d %10d\n",a[k],b[k]);
fprintf(fl,"%3d %10d\n",a[k],b[k]);
}
fprintf(fl,"\n");
printf("\n");
}
int searchInt(int a[],int ctr,int idnm)
{
int k;
int loc=-1;
for(k=0;(k<ctr)&&(loc==-1);k++)
if(a[k] == idnm)
loc = k;
return loc;
}