943,885 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1538
  • C RSS
Sep 1st, 2006
1

whats wrong with my code cant display result

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

help me please

 
#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();
}
Similar Threads
Reputation Points: 16
Solved Threads: 0
Light Poster
vbcielle is offline Offline
26 posts
since Sep 2006
Sep 1st, 2006
1

Re: whats wrong with my code cant display result

First of all fflush(stdin);won't do the work!
Read this and this
Last edited by andor; Sep 1st, 2006 at 9:02 am.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Sep 1st, 2006
0

Re: whats wrong with my code cant display result

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
  1. struct cgrade
  2. {
  3. char course[50]; // room for 50 character course name
  4. float credit;
  5. float grade;
  6. };

>>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
  1. fgets(GPA[i].course, sizeof(GPA[i].course), stdin);
Last edited by Ancient Dragon; Sep 1st, 2006 at 9:02 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,952 posts
since Aug 2005
Sep 2nd, 2006
1

Re: whats wrong with my code cant display result

thanks already solved
Reputation Points: 16
Solved Threads: 0
Light Poster
vbcielle is offline Offline
26 posts
since Sep 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: <map> find function
Next Thread in C Forum Timeline: Query





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC