I/O GPA Calculator -- Not Storing All of File

"Write a program that reads from a file a list of course names, letter grades for each course and number of credits each course is worth. The program will read each course name, grade earned and number of credits from the file and print out the course name, grade earned and number of credits for each course to the screen. After the program has read in all of the courses, grades earned and credits,the program should calculate the grade point average based on all of the courses read in by the program."

Input File:
1
2
3
4
5
6
Ariel Lewis
Biology A 4.0
Statistics C 4.0
History A 3.0
Tennis	A 1.0
English	B 3.0




So far I have written this much.

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;
}


My main issue is that right now, the code is reading the LAST set of information in the input file and using that to calculate the GPA. How do I get my code to read and store all of the information from the file being read? Am I supposed to use arrays?
1
2
while (fscanf(spGrades, "%s %c %f \n", &course, &grade, &credits) != EOF)
		printf("%15s      %3c     %12.1f \n\n", course, grade, credits);\

You are repeatedly reading from the file and overwriting with the info just previously read.
After this, you close the file and your variables contain the latest info.
That is why you only manipulate the last piece of info from the file.

Last edited on
No. You dont need an array.
Algo:
1
2
3
4
5
6
7
8
- totalCredit = 0;
-count = 0; //number of courses read
-while(!eof)
   -print name, grade, credit
   -totalCredit = credit * point;
   -totalTOTALcredit  += totalCredit;
   -count++;
-gpa = totalTOTALcredit/count
Edited per your instruction:

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
while (fscanf(spGrades, "%s %c %f \n", &course, &grade, &credits) != EOF)
	{
		printf("%15s      %3c     %12.1f \n\n", course, grade, credits);
		totalCredits = credits*totalPoints;
		totalTOTALcredits += totalCredits;
		count++;
	}
	GPA = totalTOTALcredits / count;

		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;
		}


Now I'm receiving an answer of 0 total credits and GPA of 0.
I know I'm missing SOMETHING I just can't place what or where!
Please! Anyone? Could you help me with what I have here?

I am still struggling with this.
Topic archived. No new replies allowed.