Quote:
>int main ()
>{
>int acct_num[10],i /*account number*/;
>int day_o[10],z /*days overdue*/;
>int count /*counter*/;
>int balance /*balance*/;
>int tot_bal /*total of all balances*/;
>int hi_bal /*highest balance*/;
>int hi_bal_acct /*highest balance account number*/;
>int lo_bal /*lowest balance*/;
>int lo_bal_acct /*lowest balance account number*/;
>int avg /*average of balances*/;
>FILE *book_data /*pointer to book.data file*/;
>book_data = fopen ("book.data","r");
>i = 0;
>z = 0;
>printf ("%Account Number%20sDays Overdue%20sBalance Due\n\n", " ");
>fscanf (book_data,"%d%d",&acct_num[i],&day_o[z]);
>while (i <= 10)
>{
>printf ("%d%d\n",acct_num[i],day_o[z]);
>i += 1;
>}
>return 0;
>}
>I'm having trouble with getting the information from the file book_data to
>load
>onto the array. Any help would be appreciated.
Some of the problems in this program are:
fscanf() is only being called once, so it only reads the first two numbers
from the file. The call to fscanf() should be within a loop.
You increment i, but neglected to increment z. Since these variables
should always have the same value, you don't need z - just use i for
indexing both arrays.
You have
i = 0;
Followed by a few other statements, then
while (i <= 0)
and inside the while loop
i += 1;
Although this will work, it would be MUCH better to use a 'for' loop.
The initialization, test, and update of i would be on one line,
instead of spread out through the program. The program would be
easier to read; more importantly, it would be less likely that future
modifications to the program would accidentally break it.
The thing I find particularly unnerving about what you have now is
that after you initialize i in preparation for the loop, you do several
other things before entering the loop. In the future, someone might
change those intervening lines to something that modifies i, without
noticing that doing so ruins the loop. (I realize that's not really
going to happen because this is a homework problem, but
homework problems are for learning, and it's better to learn good
habits than bad ones.)
If you don't change to a 'for' loop, then at the very least,
i = 0;
should be moved to the line _immediately_ before
while (i <= 10)
You don't check the return status from any of the library calls.
That's OK for printf(). Missing, misplaced, or misnamed data
files are _very_ common problems, so you should _always_
check the result from fopen():
book_data = fopen ("book.data","r");
if (book_data == NULL) {
/* output informative message here with perror(), printf(), or
fprintf(stderr, ) */
return EXIT_FAILURE;
}
You should check the results from fscanf() as well. I never use
fscanf(), so I will leave it to you or another respondent to figure
out the details.
Since you use standard library functions, you should include the
corresponding standard header file:
#include <stdio.h>
If you use my suggested code above for checking the return value
from fopen(), you will need the definition of EXIT_FAILURE, so
you will also need:
#include <stdlib.h>
Finally a couple of style tips:
You have:
i += 1;
which is OK, but
i++;
or
++i;
is generally considered better.
A consistent indenting scheme would make the program easier to
read. (But inconsistent indenting is worse than none, so your
program is a lot easier to follow than some I've seen.)
-- Gary Culp
--