1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#define _CRT_SECURE_NO_WARNINGS /*fopen continued to cause the error "use fopen_s",
and upon using fopen_s another error would occur saying "function does not take 2 arguments".*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int dummy;
FILE* spGrades;
float credits;
float GPA;
char name;
char grade;
char course[25];
char string[60];
float count = 0;
float totalPoints = 0;
float totalCredits = 0;
printf("This program will read and print the students name, the course names,\n");
printf("the letter grades of that course and the number of credits each course is worth.");
printf("It will then calculate the GPA of the courses.");
spGrades = fopen("grades.txt", "r");
if (!spGrades)
{
printf("\n\nCould not open file grades.txt \a\n");
exit(101);
}
fgets(string, 60, spGrades);
printf("\n\nStudent name: %s\n\n", string);
printf(" |Course Name| |Grade Earned| |Credits Hours|\n\n");
while (fscanf(spGrades, "%s %c %f \n", &course, &grade, &credits) != EOF)
printf("%15s %3c %12.1f \n\n", course, grade, credits);\
fclose(spGrades);
if (grade == 'A'){
totalPoints += 4.0;
}
else if (grade == 'B'){
totalPoints += 3.0;
}
else if (grade == 'C'){
totalPoints += 2.0;
}
else if (grade == 'D'){
totalPoints += 1.0;
}
else if (grade == 'F'){
totalPoints += 0.0;
}
totalCredits = credits*totalPoints;
GPA = totalCredits / totalPoints;
printf("----------------------------------------------------------------------\n");
printf("\nThe Total Credits earned by this student are: %.1f \n", totalCredits);
printf("\nThe GPA for this Student is: %.1f \n\n\n", GPA);
printf("----------------------------------------------------------------------\n");
printf("\n\n\nThe program has finished. Press enter to exit. \n");
scanf_s("%d", &dummy);
return 0;
}
|