[I write the program in C language]
I would like to read a txt file containing StudentName, Score, and Remarks
The pattern of the txt file looks like this:
1,Adam Lambert,60,C
2,Josh Roberts,100,A
3,Catherine Zetta,80,B
I would like to store each data, respectively into an array
So I would have 3 arrays (to store StudentName, Scores, and Grades)
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <math.h>
char* studName[99];
char* grade[99];
char* grades[100][10];
char buff[1024];
int scores[100];
int score;
int index;
int size;
int index = 0;
int sum = 0;
double average = 0;
int main()
{
FILE *file;
file = fopen("notepad.txt", "r");
if(file == NULL){
printf("Data does not Exist");
}
else {
while(((fgets(buff,1024,file))!=NULL))
{
size = strlen(buff);
buff[size] = 0;
sscanf(buff, "%d, %[^,],%d, %[^,]", &index, studName,&score, grade);
printf("Student Name: %s\n", studName);
printf("Score: %d\n", score);
printf("Remark: %s\n", grade);
scores[index-1] = score;
grades[index-1][10] = grade;
sum+=score;
index++;
}
}
fclose(file);
average = sum/index;
printf("\nThe total score of student is: %d\n", sum);
printf("\nThe sum of student is: %d\n", index);
printf("\nThe average of students' score is: %2f\n", average);
for(int i = 0; i<3; i++){
printf("Score: %d\n", scores[i]);
printf("\nThe Remark is: %s\n", grades[i][10]);
}
getch();
return 0;
}
The code above had successfully stored the scores in int array. I had not really good in C, so I do not know how to store the char array for StudentName and Grades.
The abovde code gives the result of Grades stored is only the last grade on the txt file (in this case is 'B').
Would you please tell me what do I have to fix in order to achieve my goal?
My goal basically will be pretty much like this:
StudentName array contains {"Adam Lambert", "Josh Roberts", "Catherine Zetta"}
Scores array contains {60,100,80}
Grades array contains {"C", "A", "B"}
Thank you very much.