Can you post your latest code?
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
Code:#include <stdio.h> #define MAX_STUDENTS 50 int getScores(int finalScores[], int stuID[]); int calculateScores(char P_N, int homeworkTotal, int midtermTotal, int final, int extraCredit); void sortScores(int finalScores[], int stuID[], int stu_num); int printToFile(int finalScores[], int stuID[], int stu_num); int main (void) { int stuID[MAX_STUDENTS]; int finalScores[MAX_STUDENTS]; int stu_num; stu_num = getScores(finalScores, stuID); sortScores(finalScores, stuID, stu_num); printToFile(finalScores, stuID, stu_num); return 0; } /*scan the students ID's into an array and scan in each students scores*/ int getScores(int stuID[], int finalScores[]) { FILE* spGrades; int final; int extraCredit; int ID; int ret; char P_N; int stu_num; if (!(spGrades = fopen ("BG_Scores.txt", "r"))) { printf("\aError opening student grade file\n"); } for(stu_num = 0; stu_num < MAX_STUDENTS; stu_num++){ // no more MAX_STUDENTS //processing student ID's ret=fscanf(spGrades, "%d\n", &ID); if(ret != 1){//until no more student ID's printf("Grades written to file successfully!\n"); return stu_num; } //process pass or no pass grade fscanf(spGrades, "%c\n", &P_N); //processing student homework scores int homeworkTotal = 0; int i; int hw; for (i = 0; i < 8; i++) { fscanf(spGrades, "%d ", &hw); homeworkTotal = homeworkTotal + hw; } fscanf(spGrades, "%d\n", &hw); homeworkTotal = homeworkTotal + hw; homeworkTotal /= 9; // processing midterms int midtermTotal = 0; int midterm; { fscanf(spGrades, "%d %d", &midterm, &midtermTotal); midtermTotal = midtermTotal + midterm; } midtermTotal /= 2; //processing final grade fscanf(spGrades, "%d\n", &final); //process extra credit fscanf(spGrades, "%d\n", &extraCredit); stuID[stu_num] = ID; finalScores[stu_num] = calculateScores(P_N, homeworkTotal, midtermTotal, final, extraCredit); } fclose(spGrades); } int calculateScores(char P_N, int homeworkTotal, int midtermTotal, int final, int extraCredit) { int final_grade; if(P_N != 'P') return 0; if(homeworkTotal <= 70) return 0; if(midtermTotal <= 60) return 0; if(final <= 60) return 0; final_grade = ((homeworkTotal + midtermTotal + final + extraCredit) / 3); return(final_grade); } void sortScores(int finalScores[], int stuID[], int stu_num) { int current; int walk; int tempData; for(current = 0; current < stu_num; current++) { for (walk = (current + 1); walk < stu_num; walk++) if(finalScores[current] < finalScores[walk]) { tempData = finalScores[current]; finalScores[current] = finalScores[walk]; finalScores[walk] = tempData; tempData = stuID[current]; stuID[current] = stuID[walk]; stuID[walk] = tempData; } } } int printToFile(int finalScores[], int stuID[], int stu_num) { FILE* spSorted; if (!(spSorted = fopen ("BG_Sorted.txt", "w"))) { printf("\aError opening student grade file\n"); } int i; fprintf(spSorted, "Spring 2012\n"); fprintf(spSorted, "CIS 15 BG\n"); fprintf(spSorted, "====\t======\t====\n"); fprintf(spSorted, "ID\tSCORE\tGRADE\n"); fprintf(spSorted, "====\t======\t====\n"); for (i =0; i < stu_num; i++){ fprintf(spSorted, "%d\t", stuID[i]); fprintf(spSorted, "%d\t", finalScores[i]); int finalScore = finalScores[i]; if(finalScore >= 97) fprintf(spSorted, "A+"); else if(finalScore >=93 && finalScore <= 96.9) fprintf(spSorted, "A"); else if(finalScore >= 90 && finalScore <= 92.9) fprintf(spSorted, "A-"); else if(finalScore >= 87 && finalScore <=89.9) fprintf(spSorted, "B+"); else if(finalScore >=83 && finalScore <=86.9) fprintf(spSorted, "B"); else if(finalScore >=80 && finalScore <= 82.9) fprintf(spSorted, "B-"); else if(finalScore >=77 && finalScore <=79.9) fprintf(spSorted, "C+"); else if(finalScore >= 70 && finalScore <= 76.9) fprintf(spSorted, "C"); else if(finalScore >= 67 && finalScore <= 69.9) fprintf(spSorted, "D+"); else if(finalScore >= 63 && finalScore <= 66.9) fprintf(spSorted, "D"); else if(finalScore >= 60 && finalScore <= 62.9) fprintf(spSorted, "D-"); else fprintf(spSorted, "F"); fprintf(spSorted,"\n"); } }
Can you provide your sample input that you are testing with in the input file and what the output file looks like?
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
Do you want both the input and output files?
Input:
1234
P
100 100 100 100 100 100 100 100 100
100 100
100
15
1234
P
100 100 100 100 100 100 100 100 100
100 100
100
15
2381
P
100 100 100 100 100 100 100 100 100
100 100
100
15
3452
N
100 100 100 100 100 100 100 100 100
100 100
100
15
4702
P
100 95 99 92 90 96 97 100 100
89 93
91
12
5488
P
100 95 90 82 97 83 70 89 99
95 52
80
10
6569
P
70 70 60 50 40 30 30 20 10
70 80
90
15
7690
P
100 95 100 96 100 97 100 98 99
50 40
65
15
8789
P
99 85 92 78 80 90 70 85 93
87 79
82
13
9890
P
100 100 86 92 76 93 100 89 89
70 80
23
15
1111
P
80 90 75 100 78 89 97 50 95
80 70
75
15
Output:
Spring 2012
CIS 15 BG
==== ====== ====
ID SCORE GRADE
==== ====== ====
0 9890 A+
87 8789 A+
0 7690 A+
0 6569 A+
84 5488 A+
96 4702 A+
0 3452 A+
105 2381 A+
105 1234 A+
105 1234 A+
82 1111 A+
Ok, I guess I am pretty tired myself, between this and shouting at some idiots at work, because I can't tell what the bloody hell is going on that is swapping the two arrays.
Let's take the following approach by making a little experiment:
In main, after the read function try to printf both arrays so that we can see what is in them. Do this after the read, but before the sort, and again after the sort but before the print. One for loop should do it:
i.e. for i = 0, i<stu_num i++ printf(ID[i],score[i])
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
-hahahaha its printing it out in reverse!Code:8 int main (void) 9 { 10 int stuID[MAX_STUDENTS]; 11 int finalScores[MAX_STUDENTS]; 12 int stu_num; 13 int i; 14 stu_num = getScores(finalScores, stuID); 15 for(i = 0; i < stu_num; i++) 16 { 17 printf("%d\n%d\n", stuID[i], finalScores[i]); 18 } 19 20 sortScores(stuID, finalScores, stu_num); 21 printToFile(stuID, finalScores, stu_num); 22 23 return 0; 24 }
Output:
105
1234
105
1234
105
2381
0
3452
96
4702
84
5488
0
6569
0
7690
87
8789
0
9890
82
1111
Omg!!!!! I got it to work!!! Thank you so much!!!
What was the problem?
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
I started fixing the problem a while ago, and then when we did the printf, it confirmed the problem and brought my attention to the line above printf. Basically, we were passing the stuID and finalScores to the functions in reverse order. (hope that makes sense). So when I switched the order for each function, it worked.
-Any other tweaks I should know about, or is this thing finally done?
I think its done. You could add an fclose() at the end of the print function.
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
Make sure you drink some coffee and don't sleep through your submission and let everything go to waste.
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
An fclose(spSorted) at the very end of the program, right?