0

I have to read in a file with inconsistent inputs. I am working on a Max-Heap and have all of the heap functions working properly. The file looks like:

5
1
2
3
4
5
I 7
E
C 3 9
E
D 2

The first number is the size of the heap. Then the remaining numbers are nodes in the heap. After this the remaining lines are operations. When the letter is "I" it will always only have one number following because it is to insert a value into the heap. When the letter is "E" it will only be to delete the root node (no following numbers). When the letter is "C" it will always have 2 numbers following because it is to change a nodes index. Lastly when the letter is "D" there will only be 1 number following and it is to delete the node at that index. Currently my code is:

while(!feof(fPtr)){
    char temp;
    int i, v, test=0;
    fscanf(fPtr, "%c", &temp);

    if(strcmp(&temp, "I")==0){
      test=1;
    }
    else if(strcmp(&temp, "C")==0){
      test=2;
    }
    else if(strcmp(&temp, "D")==0){
      test=3;
    }
    else{
      test=4;
    }

    switch(test){
      case 1:
        //if the character is I then read in the key
        fscanf(fPtr, " %d\n", &v);
        //insert new key v
        heapSize = insert(myHeap, heapSize, v);
        buildMaxHeap(myHeap, heapSize);
        break;
      case 2:
        //if the character is C then read in the index and key
        fscanf(fPtr, " %d %d\n", &i, &v);
        //change the key of the heap at index i to a new key
        heapChangeKey(myHeap, heapSize, i-1, v);
        buildMaxHeap(myHeap, heapSize);
        break;
      case 3:
        //if the character is D then read in the index
        fscanf(fPtr, " %d\n", &i);
        //delete key at index i in the heap
        heapSize = delete(myHeap, heapSize, i-1);
        buildMaxHeap(myHeap, heapSize);
        break;
      case 4:
        fscanf(fPtr, "\n");
        heapSize = delete(myHeap, heapSize, 0);
        buildMaxHeap(myHeap, heapSize);
        break;
    }
  }

Why does this code not read in the letters correctly?

joycem8845
  • 83
  • 9
  • 1
    At first grance, your usage of `while(!feof(fPtr))` is [wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) and you should check if reading are successful *before* using what are "read". – MikeCAT Nov 14 '20 at 00:16
  • You haven't actually asked a question or described a specific error/problem with the code. – kaylum Nov 14 '20 at 00:18
  • I would remove *all* the whitespace from the format strings, except for where it is `%c` which needs a leading space. – Weather Vane Nov 14 '20 at 00:18
  • 3
    One major issue: `temp` is a `char`. You can't use `strcmp` on a single char. It needs to be a string which is a NUL terminated sequence of chars. To compare a single char use `temp == 'C'` – kaylum Nov 14 '20 at 00:18
  • @kaylum the use of strcmp seemed to be the issue. Thanks! – joycem8845 Nov 14 '20 at 00:27
  • Glad you found your issue. When you eliminate the `strcmp`, you can just do (e.g.): `switch (temp) { case 'I': ... break; case 'C': ... break; case 'D': ... break; default: ... break; }` – Craig Estey Nov 14 '20 at 00:41

0 Answers0