I'm working on a priority queue for an algorithms class homework, using C. The priority queue is working and not a problem. However, we have to read lines from a file containing a string/char array, and a number for the priority. Here is my main
function that does all of that:
int main() {
p_queue *q = make_queue(2);
FILE *of = fopen("sportsball2.txt", "r");
char buffer[128]; //no name is longer than this
char *part, *assign;
int x;
while(!feof(of)) {
fscanf(of, "%s\n", buffer);
if(strcmp("GO!", buffer) != 0) {
part = strtok(buffer, "/");
assign = part;
part = strtok(NULL, "/");
x = atoi(part);
insert(q, assign, x);
} else {
entr_struct *t = pop(q);
if(!t) {
printf("No players waiting to enter!");
} else {
printf("%s%s\n", t->entry, " enters the game!");
}
}
}
destroy_queue(q); //calls a custom memory free function
return EXIT_SUCCESS;
}
It all works as expected unless the else
triggers, in which case the pop
returns that the entry at the top of the queue had the string "GO!" (the contents of buffer
if the else
branch is taken). I have specifically checked and made sure that the correct values are making their way into the assign
and x
variables, so I have no idea what is happening here. I am relatively new to C, so it's entirely possible that I'm missing something well-known and obvious.