I'm trying to make the program read a text file, which contains info like "1001 name surname 10 20 30", the number of the students, name, surname and 3 of his grades. Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
int number;
char name[30];
char surname[30];
int midterm1,midterm2,midterm3;
} Student;
int comp(const void * aa, const void * bb)
{
struct Student a = *(struct Student*)aa;
struct Student b = *(struct Student*)bb;
if (a.midterm1==b.midterm1)
return 0;
else if (a.midterm1 < b.midterm1)
return -1;
else
return 1;
} // comp
int main(void)
{
int choice,studentnumber,midterm1,midterm2,midterm3,i,n;
char surname;
FILE *cfPtr;
struct student *name;
name = malloc( 10 * sizeof(Student));
if ((cfPtr = fopen("grades.txt", "r")) == NULL)
return 1;
const int STUDENTSMAX = 100;
struct Student students[STUDENTSMAX];
char buff[1024];
while(1)
{
memset(buff, 0, sizeof(buff));
fgets(buff, sizeof(buff) -1, cfPtr);
if (feof(cfPtr)) {
break;
}
sscanf(buff, "%d %s %s %d %d %d", &students[i].number, students[i].name, students[i].surname, &students[i].midterm1, &students[i].midterm2, &students[i].midterm3);
printf("%4d %15s %15s %10d %10d %10d\n", students[i].number, students[i].name, students[i].surname, students[i].midterm1, students[i].midterm2, students[i].midterm3);
i++;
} // while
while (!feof(cfPtr))
{
fscanf(cfPtr, "%d%s%s%d%d%d", &students[i].number, &students[i].name,&students[i].surname, &students[i].midterm1, &students[i].midterm2, &students[i].midterm3);
printf("%4d%15s%15s%10d%10d%10d\n", students[i].number, students[i].name,students[i].surname, students[i].midterm1, students[i].midterm2, students[i].midterm3);
i++;
} // while
printf("What would you like to do? \n"
"1- Sort according to midterm 1\n"
"2- Sort according to midterm 2\n"
"3- Sort according to midterm 3\n"
"4- Exit\n");
scanf("%d",&choice);
scanf("%d",&choice);
switch (choice) {
case 1:
qsort(students, i, sizeof(struct Student), comp);
for (n = 0; n < i; n++) {
printf("%4d %15s %15s %10d %10d %10d\n", students[n].number, students[n].name, students[n].surname, students[n].midterm1);
} // for
break;
case 2:
qsort(students, i, sizeof(struct Student), comp);
for (n = 0; n < i; n++) {
printf("%4d %15s %15s %10d %10d %10d\n", students[n].number, students[n].name, students[n].surname, students[n].midterm2);
} // for
break;
case 3:
qsort(students, i, sizeof(struct Student), comp);
for (n = 0; n < i; n++) {
printf("%4d %15s %15s %10d %10d %10d\n", students[n].number, students[n].name, students[n].surname, students[n].midterm3);
} // for
break;
} // switch
} // main?
fclose(cfPtr);
system("PAUSE");
return 0;
} // the editor failed to find the corresponding '{' !OP should fix this
This current code returns me with this error:
95 C:\Users\UseR\Desktop\main.c [Warning] parameter names (without types) in function declaration
95 C:\Users\UseR\Desktop\main.c [Warning] data definition has no type or storage class
98 C:\Users\UseR\Desktop\main.c syntax error before string constant
98 C:\Users\UseR\Desktop\main.c [Warning] data definition has no type or storage class
C:\Users\UseR\Desktop\Makefile.win [Build Error] [main.o] Error 1
The program is compiling and working without any problems, yet it still gives the error and doesn't sort at all. What am I doing wrong here?
EDIT: Here is my imput file;
100201 al beaver 40 50 70
100202 andrew matthews 30 90 75
100203 leah doga 60 55 80
100204 rob kurt 45 80 60
100205 aliah devon 65 70 50
100206 sally pir 70 40 85
100207 eric bekta 75 65 55
100208 nile coul 55 75 65
100209 mina umur 72 60 90
100210 john hot 73 63 87
P.S. Yes, this is a homework before anyone asks, I'm running out of time and I've been really trying for days without any solid progression. The thing I'm asked mainly was "
- Sort grades of chosen exam in decreasing order and output file will contain only three columns: names,surnames and scores of chosen exam.
- Define a structure named “student”, to store name,surname and scores of each student under the same variable name. So all data in the file will be stored in an array of new data type named “student”."