Hi All,
I believe its going to be a long night. This is a pretty long program (for me) and Im only about 1/2 way thru. Right now Im stuck because somehow one of my variables is getting changed and I cant figure out how. Ive been using this site as a resource for awhile now as I struggle thru my C courses, desperately trying to understand. I knew it was only a matter of time before I turned here for help. So, here goes...
NOTE: You will see a lot of printf statements that are left justified. These are for debugging. Sorry! But I left them in so it is also easier to tell you where the problem is. Somehow between printf("NumElems6:) and printf("NumElems7:) my numElems variable is getting changed from 10 to 4. Im using an input file (obviously) and will try to upload it, as I imagine it will probably help you see what is going on.
Thanks so much in advance for your help!
Description: Program reads from input file # of test
answers, correct test answers, Student ID, and student's
answers, checks to see if student got answer right, and
then prints the results.
Code:
#include<stdio.h>
int readAnswerInfo (FILE *infilep, int numElems);
int readStudentData(FILE *infilep, int numElems, int answerKeyArray[], int frequencyArray[], FILE *outfilep);
int main (void)
{
int numElems = -1; /*number of test questions and
return variable from readAnswerInfo (function 1)*/
FILE *infile; /*declares infile as a file variable*/
FILE *outfile; /*declares outfile as a file variable*/
int answerKeyArray[] = {0};
int frequencyArray[] = {0};
printf("NumElems1: %d\n", numElems);
infile=fopen("test_input1.txt", "r");
if( infile == NULL )
{
printf("Input file didn't open\n");
return 100; //exit main
} //end
printf("NumElems2: %d\n", numElems);
outfile=fopen("output.txt", "w");
if(outfile==NULL)
{
printf("Output file didn't open\n");
return 101; //exit main
} //end
printf("NumElems3: %d\n", numElems);
numElems = readAnswerInfo(infile, numElems);
printf("NumElems4: %d\n", numElems);
if(numElems > 0 || numElems <= 100)
{
readStudentData(infile, numElems, answerKeyArray,
frequencyArray, outfile);
}
else
printf("Error with input file: input1.txt\n");
printf("\nNumElems8: %d\n", numElems);
printf("\nAnswer Key: ");
for( int i = 0; i < numElems ; i++ )
printf("%d", answerKeyArray[i]);
return 0;
}
int readAnswerInfo (FILE *infilep, int numElems)
{
fscanf(infilep,"%d", &numElems);
printf("NumElems5: %d\n", numElems);
if( numElems > 0 || numElems <= 100 )
return numElems;
else
return 0;
}
int readStudentData (FILE *infilep, int numElems, int answerKeyArray[], int frequencyArray[], FILE *outfilep)
{
int i;
printf("NumElems6: %d\n", numElems);
for( i = 0; i < numElems ; ++i )
fscanf(infilep,"%d", &answerKeyArray[i]);
printf("\nNumElems7: %d\n", numElems);
printf("Answer Key: ");
for( i = 0; i < numElems ; i++ )
printf("%d", answerKeyArray[i]);
return 0;
}

