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

Recommended Answers

All 3 Replies

First of all fflush(stdin); won't do the work!
Read this and this

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

thanks already solved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.