Re: I went a help
- From: Barry Schwarz <schwarzb@xxxxxxxxx>
- Date: Fri, 17 Mar 2006 17:27:23 -0800
On Fri, 17 Mar 2006 05:06:17 -0600,
h_u_s2002@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (join) wrote:
that what i did auntil now but i cause i problem , the output of this
code id
null 0.000 0
How did you execute the code when it has syntax errors? Is this your
real code or did you retype it? You should always use cut and paste.
what the problem here?
also, what is the best way of finding the max,min ,average of the
gpa?
code:
#include <stdio.h>
#include <stdlib.h>
typedef struct student {
char *name;
int id;
float gpa;
} stu;
stu nig[5];
int main(){
int i,n,avg,min,max;
int id;
float gpa;
char name[5];
FILE *inputFile;
inputFile = fopen("q4.txt","r");
if (inputFile) printf("File succesfully opened for
reading!\n");
else {
printf("File not opened\n");
exit(1);
}
for(i=0;i<5; i++){
fscanf(inputFile, "%s %d
%f",nig.name,nig[i].id,nig[i].gpa);
nig.name is syntactically incorrect. You need nig[i].name, just like
with id and gpa. nig is an array of struct. nig[i] is the i-th
struct in the array. And nig[i].name is the pointer to char in that
struct.
Unfortunately, you have an additional problem. nig is a global array.
It is initialized by default such that each element of the array (in
this case that means each member of each element) is set to the
correct form of zero. Since name is a pointer, name is initialized to
NULL. Your call to fscanf directs that function to store the input
name there. You are not allowed to store data where a NULL pointer
points. You need to initialize all five nig[i].name pointers to point
somewhere in memory that belongs to your program.
printf("are:%s%d,%f",nig[i].name,nig[i].id,nig[i].gpa);
You should add a \n to the end of your format string.
//}
fclose(inputFile);
}
return 0;
}
Remove del for email
.
- References:
- I went a help
- From: join
- re:I went a help
- From: join
- I went a help
- Prev by Date: Re: how to count rows and columns of integers/doubles in a file?
- Next by Date: Re: i++ * i++
- Previous by thread: Re: I went a help
- Next by thread: re:I went a help
- Index(es):
Relevant Pages
|