Please help can you help me debug my code i cant display the result and the computation for sum is error...

help me please :sad:

#include <stdio.h>
 int i,counter;
float sum;
 struct cgrade
 {
    char course[50];
    float credit[50];
    float grade[50];
 };
struct cgrade GPA[50];
main(){
 
 printf("Enter number of courses to input:");
 scanf("%d",&counter);
 
sum=0;
 for(i=0;i<counter;i++){
 printf("Enter course:");
 fflush(stdin);
 gets(GPA[i].course);
 printf("Enter credit:");
 scanf("%f",&GPA[i].credit);
 printf("Enter grade:");
 scanf("%f",&GPA[i].grade);
sum=sum+GPA[i].credit;
 }
 
printf("Sum: %f",sum);
 
 for(i=0;i<counter;i++){
 printf("Course %s\n",GPA[i].course);
 printf("Credit %f\n",GPA[i].credit);
 printf("Grade %f\n",GPA[i].grade);
 }
 
  getch();
}

look at the structure -- credit is an array of floats and you are attempting to use it as if it were a single float object. Change the structure to remove the arrays

struct cgrade
 {
    char course[50]; // room for 50 character course name
    float credit;
    float grade;
 };

>>gets(GPA.course);
never, ever use gets() function because it can cause your program to crash if you enter more characters than the input buffer can hold. Use fgets() instead

fgets(GPA[i].course, sizeof(GPA[i].course), stdin);
This question has already been answered. Start a new discussion instead.