简体   繁体   English

代码不会在C中的do while循环中执行

[英]Code won't execute in do while loop in C

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: 然后是部分GDB日志:

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:"); 如您所见,第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 我在命令中输入d

Could someone help me explain this? 有人可以帮我解释一下吗? Thanks. 谢谢。

I think your concern is that the printf is traced in the debugger but no output is produced. 我认为您担心的是在调试器中跟踪了printf ,但未生成任何输出。 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. 这可能是因为printf调用将输出发送到通常缓冲的stdout ,所以直到稍后在gdb中运行时,输出才可能出现。 In some systems, the buffer is flushed when a newline is seen. 在某些系统中,看到换行符时将刷新缓冲区。 So you might try adding \\n to the end of "Queue:" . 因此,您可以尝试将\\n添加到"Queue:"的末尾。 Or fflush(stdout); fflush(stdout); after the print will definitely cause the printf to work. 打印后肯定会导致printf工作。 Output to stderr is not buffered. stderr未缓冲。 That's why you see that output immediately. 这就是为什么您立即看到该输出的原因。

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. 它不会立即打印,因为printf()输出一直保存在缓冲区中,直到缓冲区已满或遇到换行符为止。 Either put a newline at the end or fflush(stdout) afterwards if you need to see the output immediately. 如果您需要立即查看输出,则在末尾添加换行符,或者在之后fflush(stdout)替换。

检查完代码后,我唯一能想到的解决方案是for循环q->end是<= 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. 我知道我的代码很乱,我仍然是C编程语言的初学者。 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. 代码的“ ...”部分进行了一些修改,但我相信这些更改与I / O不相关。

Thank you all for suggestions. 谢谢大家的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM