0

I have to do a program for class that is using parallel arrays to store data of a students grades. We are to give the arrays data by using the Linux command "<" when executing the program.

./a.out < inputfile

However, when I run the program I get an infinite loop of the menu that program is using to access the data in the arrays. It looks like the program is skipping the function I'm using to populate the arrays from the function and using the data from the file for the menus scanf() that is in a separate function.

Here is the function that I'm using to populate the arrays:

void populate(int idarray[],int testone[], int testtwo[],float hw[],int max)
{   
    int counter = 0;
    while(scanf("%d", &idarray[counter]) != EOF)
    {
        if(idarray[counter] != -1)
        {
            //scanf("%f %d %d", &hw[counter], &testone[counter], &testtwo[counter]);

            scanf("%f",&hw[counter]);
            scanf("%d",&testone[counter]);
            scanf("%d",&testtwo[counter]);
            counter++;
        }
        if(counter == max-1)
           break;
   }
}

"max" is the bounds of the arrays.

the input file would look like:

1983990 75.6 39 78 1935440 50.03 78 34

turbo
  • 1,233
  • 14
  • 36
Andrew B.
  • 167
  • 2
  • 10
  • Just wondering whether your while loop breaks because of counter == max - 1 or by EOF. If it is by the former then you will still be having inputs in stdin which will be given as input to your menu choices without your control – Titus Oct 31 '14 at 19:17
  • Can you give a (small) example of the input? And have you stepped through the code line by line in a debugger, and checked what each `scanf` call returns? – Some programmer dude Oct 31 '14 at 19:18
  • I added the counter==max -1 more as a debug because i was originally getting segmentation fault:11 as an error at runtime. – Andrew B. Oct 31 '14 at 19:23
  • I haven't used a debugger before so and i dont know of any. – Andrew B. Oct 31 '14 at 19:24
  • this line: while(scanf("%d", &idarray[counter]) != EOF) has the problem that there is no check to assure that a value was actually read. AND will probably commence to failing when it gets to the end of the first row in the input file. There are a couple of accumulative fixes. 1) modify the format string to " %d" so white space, including the newline character(s) are skipped over. 2) modify the line to: while( (status = scanf("%d", &idarray[counter])) != EOF) then status can be checked against 1, where 1 is the number of parameters that should be filled on each call to scanf() – user3629249 Nov 01 '14 at 04:21
  • similar considerations and formatting should be applied to the other calls to scanf() – user3629249 Nov 01 '14 at 04:22
  • this line: if(idarray[counter] != -1) means if the value read for idarray[counter] is -1 then skip reading the other 3 variables, BUT stay in the loop and read another idarray[counter] value. Is this really the expected situation (your comments did not state it) that a id of -1 will not be followed by the other 3 inputs? – user3629249 Nov 01 '14 at 04:28

2 Answers2

1

Check whether your while loop breaks because of counter == max - 1 or by EOF. If it is by the former then you will still be having inputs in stdin which will be given as input to your menu choices without your control

Explanation: If your while loop breaks because of "counter == max -1" that means your "max"(array size) is lesser than the total number of inputs(decimal numbers) present in your input file. If your "max"(array size) is appropriate then it will break only by EOF. Now if the while breaks before reaching EOF, that means you have not consumed all of the input decimal numbers you have passed to stdin, meaning there will be input numbers left to be consumed even after termination of your while loop. This yet to be consumed inputs will be consumed (in other words will be given as input to) by your menu choice's "scanf" (which get the choice for the next action). That is the reason for the cursor not waiting for you to give input to your menu choice

You can also forcefully clear your stdin buffer by following the answer( I am not able to flush stdin), just before calling your menu function, so that it will wait for your input choice

Community
  • 1
  • 1
Titus
  • 907
  • 8
  • 18
  • I just checked if it was exiting because of couter == max-1 or EOF and it was exiting because of EOF. Im not sure what that means. – Andrew B. Oct 31 '14 at 19:35
  • You mentioned "added the counter==max -1 more as a debug because i was originally getting segmentation fault:11 " Is the original segmentation fault fixed now? since you were saying it is terminating because of EOF now – Titus Oct 31 '14 at 19:45
  • If your code is terminating with EOF, I don't see why your menu choice is getting skipped automatically, since stdin is empty by that time – Titus Oct 31 '14 at 19:48
  • yes, the segmentation fault is fixed. The menu is in an infinite loop. I dont know where it is getting input from, im going to add a print statement to see what value it is reading in for the menu. – Andrew B. Oct 31 '14 at 20:01
0

I don't see anything deeply wrong with your function, but your termination criterion appears to be off by one. Off by two, really, if it is tested where you are testing now. Think about what happens when max is 0 or 1.

To accommodate the possibility that max is 0, you need to test the termination condition (correctly) at the very beginning of each loop iteration, before reading anything.

Also, I tend to prefer testing inequalities instead of equalities, as that does a better job of reigning misbehavior when I get my indexing a bit wrong. So in this case I would either break when counter >= max or perform an iteration only when counter < max.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157