I'm writing a grade book program for school and I'm confused about one thing. We have a file to read from that has multiple student IDs and multiple scores. Would doing this:
results = fscanf(gradebook, "%d %d %d %d", id, sco1, sco2, sco3);
store the number into those variables as it reads then stop the cursor when it runs out of variables to store the information in?...then should I jump right into a calculation function to calculate the final grade before having it loop the fscanf for the next student?
results = fscanf(gradebook, "%d %d %d %d", id, sco1, sco2, sco3);
getCalc(sco1, sco2, sco3);
Is that allowed? Thank you for your help.
In response to your first and second questions: The type of
id
,sco1
,sco2
andsco3
should beint *
(that is, a pointer to anint
), these variables should explicitly point to actualint
objects and you should check the return value (results
) before you use them. For example:Furthermore, getCalc should accept arguments of the type
int *
. With all of these requirements fulfilled, the answer to your third question is: Yes, you can callgetCalc
withsco1
,sco2
andsco3
as arguments.As you have probably guessed, the intermediate pointer variables are unnecessary here, and this can be simplified. However, this simplification requires a modification to your
fscanf
expression (the insertion of&addressof
operators):Which book are you reading?
The following is a working example giving the average grade for every student id:
No, unless it is in a while loop, it stops exactly after reading the four entries ONCE, unless you are using the value from
results
to control a while loop and call fscanf() multiple times to scan 4 entries at every call.Example:-