954,132 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

whats wrong with my code cant display result

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();
}
vbcielle
Light Poster
26 posts since Sep 2006
Reputation Points: 16
Solved Threads: 0
 

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

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 

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[i].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);
Ancient Dragon
Retired & Loving It
Team Colleague
30,040 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

thanks already solved :)

vbcielle
Light Poster
26 posts since Sep 2006
Reputation Points: 16
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You