| | |
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 * ansi append array arrays asterisks bash binarysearch centimeter changingto char character convert copyimagefile cprogramme creafecopyofanytypeoffileinc createprocess() database dynamic execv fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include infiniteloop initialization input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation overwrite owf pdf pointer pointers posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming standard strchr string suggestions systemcall test testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






