whats wrong with my code cant display result

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2006
Posts: 26
Reputation: vbcielle is an unknown quantity at this point 
Solved Threads: 0
vbcielle vbcielle is offline Offline
Light Poster

whats wrong with my code cant display result

 
1
  #1
Sep 1st, 2006
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();
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: whats wrong with my code cant display result

 
1
  #2
Sep 1st, 2006
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.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: whats wrong with my code cant display result

 
0
  #3
Sep 1st, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 26
Reputation: vbcielle is an unknown quantity at this point 
Solved Threads: 0
vbcielle vbcielle is offline Offline
Light Poster

Re: whats wrong with my code cant display result

 
1
  #4
Sep 2nd, 2006
thanks already solved
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC