I have written this code to take a student's Letter grade and calculate their GPA. When I run the program, I can get the student's name and the subjects correctly, but I cannot get the grades or GPA to show up.
Sample Input:
Sally
1
A
N
Sample Output:
Enter Student Name: Sally
How many subjects: 1
Grades for subject 1: A
Student Name: Sally
Grade: 4.00
Do you want to enter another student? N
Here is the code snippet I have tried.
#include <stdio.h>
int main()
{
char studentName[20];
char grade[10];
int k1;
int totalSub1,totalGrade;
float gpa1;
char ch1;
do
{
//Student Name
scanf("%s",studentName);
printf("Enter Student Name: %s\n", studentName);
//Number of subjects
scanf("%d",&totalSub1);
printf("How many subjects: %d\n",totalSub1);
//Get Grades
for(k1=0;k1<totalSub1;k1++)
{
printf("Grades:\n%c",grade[k1]);
scanf("%c",&grade[k1]);
}
//Find the gade points
for(k1=0;k1<totalSub1;k1++)
{
totalGrade=0;
switch(grade[k1])
{
case 'A':
totalGrade+=4;
break;
case 'B':
totalGrade+=3;
break;
case 'C':
totalGrade+=2;
break;
case 'D':
totalGrade+=1;
break;
case 'F':
totalGrade+=0;
break;
}
}
//Calculate GPA
gpa1=totalGrade/totalSub1;
//Print Student name with GPA
printf("Student Name: %s\n",studentName);
printf("Grade: %.2f\n",gpa1);
printf("Do you want to enter another student?\n");
scanf("%c",&ch1);
}while(ch1=='Y'||ch1=='y');
getchar();
return 0;
}