Originally Posted by
tabstop
This gives you ... one number each. If you want more, you need to malloc more memory than this.
Well, it may be so that it doesn't allocate enough, but it still doesn't solve my problem.
I am still for some reason getting data that is supposed to be in stid[] inside of stsc[].
If you want, you can take a look at my whole function right now.
Code:
int main(int argc, char* argv[])
{
int* stid = (int*)malloc(sizeof(int*));
int* stsc = (int*)malloc(sizeof(int*));
char* input_file = (char*)malloc(sizeof(char*));
char* buffer = (char*)malloc(sizeof(char*));
FILE* pfile;
int i = 0;
float avg = 0.0;
int avgtot = 0;
int sort;
int j;
printf("Please enter a file to read "
"(default is scores.txt): \n");
scanf("%s", input_file);
printf("If you would like sort the data, type the corresponding numerical value:\n");
printf("(ID = 0) ");
printf("(Grade = 1) ");
printf("(None = 2)\n");
scanf("%d", &sort);
pfile = fopen(input_file, "r");
if (pfile == NULL)
{
printf("File could not be read, reverting to default.\n");
pfile = fopen(default_file, "r");
}
if (pfile == NULL)
{
printf("Error finding scores.txt\n");
return 1;
}
printf("\nStudent Letter grades:\n\n");
printf("Student ID\t\tGrade\n");
while (fgets(buffer, 2048, pfile) != NULL)
{
sscanf(buffer, "%d%d", &stid[i], &stsc[i]);
printf("%d\t\t%s\n", stid[i], parsegrade(stsc[i]));
avgtot += stsc[i];
i++;
}
avg = (avgtot / i);
printf("\nAverage grade: %2.1f %s\n", avg, parsegrade(avg));
for(i = 0; i < 5; i++)
{
printf("%d\n", stsc[i]); //This is my test loop that is drawing strange results
}
fclose(pfile);
free(stid);
free(stsc);
free(buffer);
free(input_file);
return 0;
}