| | |
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:
Solved Threads: 0
Please help can you help me debug my code i cant display the result and the computation for sum is error...
help me please
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();
} 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
>>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
C Syntax (Toggle Plain Text)
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
C Syntax (Toggle Plain Text)
fgets(GPA[i].course, sizeof(GPA[i].course), stdin);
Last edited by Ancient Dragon; Sep 1st, 2006 at 9:02 am.
![]() |
Similar Threads
- How to Display Result In The New Form? (VB.NET)
- Display Result In Another TextBox (Visual Basic 4 / 5 / 6)
- How to make windows form from c++ code (GUI) (C++)
- Why Wont my If Work (C++)
- C problem in strings and recursive check (C)
- Great what did I miss? (C++)
- Whats wrong with my computer??? (Viruses, Spyware and other Nasties)
- no post,no display, and cpu reaching 60 degrees (Motherboards, CPUs and RAM)
- Whats wrong with this class??? (C++)
Other Threads in the C Forum
- Previous Thread: <map> find function
- Next Thread: Query
| Thread Tools | Search this Thread |
#include adobe ansi array arrays asterisks binarysearch calculate centimeter changingto char convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware highest histogram inches include incrementoperators input iso kernel km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf scheduling scripting segmentationfault sequential shape socket socketprograming stack standard string strings structures systemcall testautomation testing threads turboc unix user variable voidmain() wab windows.h windowsapi






