0

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.

hlx98007
  • 241
  • 5
  • 15
  • 3
    Er... Because you `break` out 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. – Daniel Daranas Oct 22 '13 at 13:33
  • I type 'd' into *command*, not 'q'. – hlx98007 Oct 22 '13 at 13:38
  • Are you sure the print statement is not executing? I would guess that q is null and your problem is q->end. You wont see the printf because of i/o flush issue. try putting fflush(stdout); after your printf – doog abides Oct 22 '13 at 13:42
  • Why are you fetching `command` at the beginning and end of every loop? Surely once per loop is sufficient? – r3mainer Oct 22 '13 at 13:43
  • Why are all these "break" answers getting upvoted? clearly that is not the issue. – doog abides Oct 22 '13 at 13:43
  • Anyway, you should begin by putting `{}` around each `if` and `else` clause. Otherwise your code is hard to follow and error prone. – Daniel Daranas Oct 22 '13 at 13:43
  • Yes, because for loop doesn't get executed, the final scanf() doesn't get executed. Well at least it didn't ask me for further input. – hlx98007 Oct 22 '13 at 13:44

4 Answers4

3

I think your concern is that the printf is traced in the debugger but no output is produced. This is probably because printf calls send output to stdout, which is usually buffered, so output may not appear until later when running in gdb. In some systems, the buffer is flushed when a newline is seen. So you might try adding \n to the end of "Queue:". Or fflush(stdout); after the print will definitely cause the printf to work. Output to stderr is not buffered. That's why you see that output immediately.

Gene
  • 46,253
  • 4
  • 58
  • 96
  • The scanf at the bottom didn't get executed, it didn't ask me for input in GDB. – hlx98007 Oct 22 '13 at 13:46
  • @hlx98007 That's also a buffering problem. The second scanf is getting the newline you typed after the first character. One way to deal with that is to say `scanf(" %c", &ch);`. The space before %c should purge all whitespace from the buffer before getting the next character. – Gene Oct 22 '13 at 13:51
  • `One way to deal with that is to say scanf(" %c", &ch);` Wonder why my answer got downvoted - was the first one to answer and point this out. – Sadique Oct 22 '13 at 13:55
3

It is executed, as you can see in your single stepping of the code in the debugger. It doesn't print right away because printf() output is kept in a buffer until the buffer is full or until a newline is encountered. Either put a newline at the end or fflush(stdout) afterwards if you need to see the output immediately.

Andreas Bombe
  • 2,450
  • 13
  • 20
0

After examining your code, the only solution I can come up with is that your condition in the for loop q->end is <= 0

smac89
  • 39,374
  • 15
  • 132
  • 179
  • That is correct. But it didn't ask me for input at the code's end inside the do-while loop. I don't know why. – hlx98007 Oct 22 '13 at 13:50
  • Yes but I don't know what kind of input your are giving the program. In terms of giving the program characters, there is a [better way](http://stackoverflow.com/a/19508850/2089675) to read characters from the stdin that ignores newline characters. I suggest this because that might be the case with your scanf is reading the newline character as well and you are thinking it is doing nothing – smac89 Oct 22 '13 at 13:57
  • You are right. I had the assumption that scanf ignore the newline I typed. I will change that. – hlx98007 Oct 22 '13 at 14:01
0

all.

I think I've figure it out how: just to write another function to print out all the results.

So the modified code is like this:

do
{
    ret = fgets(buf, BUF_LENGTH, "%c %d", &command, &add);
    if (ret != 1 && ret != 2)
    {
        fprintf(stderr, "Invalid input!\n");
        continue;
    }

    if (command == 'd')
    {
        ...
    }
    else if (command == 'e')
    {
         ...
    }
    else if (command == 'q')
        break;
    else
        fprintf(stderr, "Invalid command!\n");

    /* Printing out the queue elements */
    print_element(q);

    printf("Command? ");
} while (command != 'q');

I know my code is messy, I am still a beginner of C programming language. I am in the progress of learning pointers.

There are some modifications in the "..." part of code, but I believe the changes are not related to I/O.

Thank you all for suggestions.

hlx98007
  • 241
  • 5
  • 15