Note that there are some explanatory texts on larger screens.

plurals
  1. POCan someone help me find a logic error in C? I'm trying to fscanf to an array and a variable.
    primarykey
    data
    text
    <p>I am writing a program that calls several other functions from main.c. In main.c, I'm to open a file of 33 ints and put the first ten into an array (a []). SIZE is ten. The 11th int is to be stored in a variable to be used later as a seach target, which is why I called it target. I have tried a few different methods of trying to get the 11th int into target. This gives me a logic error. Could someone explain to me where the error is? My understanding is that a logic error makes syntactical sense, but something in the code doesn't make sense, like trying to use a verb instead of a preposition. I'm not sure if I'm too new at this to see the error or if I've been staring at it for too long. The output shows the array is filled correctly, but the target is always 0, so the 11th int gets placed in the next array. The code is just a fragment.</p> <p>Thanks for any help!!</p> <pre><code>int main (void) { int a[SIZE]; int i; int j; int trgt[1]; int target; FILE* fpmyfile; int closeResult; printf("Function working 1.\n"); fpmyfile = fopen("MYFILE.DAT", "r"); // open file myfile printf("Function opening file.\n"); if(!fpmyfile) { printf("Could not open input file.\n"); exit (101); } else printf("The file opened.\n"); printf("Starting for loop.\n"); for(j = 1; j &lt;= 3; j++) { for(i = 0; i &lt;= SIZE - 1; i++) //get ints from myfile into array { if(i != SIZE) fscanf(fpmyfile, "%d", &amp;a[i]); else if(i = SIZE) fscanf(fpmyfile, "%d", &amp;trgt[i]); } target = trgt[1]; printf("Scan a done.\n"); printf("\nScanned into a[]"); printf("Target is %3d\n.", target); //This print statement says that target is 0 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload