Here is a snippet of my code:
printf("\nCommand? ");
ret = scanf("%c", &command);
do
{
// printf("Command? ");
// ret = scanf("%c", &command);
if (ret != 1)
{
fprintf(stderr, "Invalid input!\n");
}
if (command == 'd')
{
result = dequeue(&queue1, &entry);
if (result == 1)
printf("%d was dequeued\n", entry);
else if (result == 0)
fprintf(stderr, "ERROR: attempt to dequeue from an empty"
" queue\n");
}
else if (command == 'e')
{
ret = scanf("%d", &add);
result = enqueue(q, add);
}
else if (command == 'q')
break;
else
fprintf(stderr, "Invalid command!\n");
printf("Queue:");
for (int i = 0; i < q->end; ++i)
{
printf("%d", q->element[i]);
}
printf("\nCommand? ");
scanf("%c", &command);
} while (command != 'q');
Then here is the partial GDB log:
146 printf("Command? ");
(gdb)
147 ret = scanf("%c", &command);
(gdb)
Command? d
148 if (ret != 1)
(gdb)
153 if (command == 'd')
(gdb)
155 result = dequeue(&queue1, &entry);
(gdb)
156 if (result == 1)
(gdb)
158 else if (result == 0)
(gdb)
159 fprintf(stderr, "ERROR: attempt to dequeue from an empty"
(gdb)
ERROR: attempt to dequeue from an empty queue
172 printf("Queue:");
(gdb)
173 for (int i = 0; i < q->end; ++i)
(gdb)
177 printf("\nCommand? ");
(gdb)
Queue:
178 scanf("%c", &command);
(gdb)
179 } while (command != 'q');
(gdb)
as you can see, the line 172 printf("Queue:"); won't get executed, as well as the rest of the code. I cannot figure out why.
I typed d into command
Could someone help me explain this? Thanks.
breakout of the loop right before? In any case, the code is messy. It should be made simpler, clearer and less error prone. Even if it worked.commandat the beginning and end of every loop? Surely once per loop is sufficient?