简体   繁体   English

如何使 do while 循环正常工作?

[英]How to make the do while loop work properly?

In this code, the second do while loop is not working properly.在此代码中,第二个do while循环无法正常工作。 Whenever I press y to enter again.每当我按y再次输入时。 It just repeats the Enter again [y/n] option again and again instead of starting from 2nd do-while loop .它只是Enter again [y/n]重复Enter again [y/n]选项,而不是从第二个do-while loop I don't want it to give me an option to enter id card number again and again.我不希望它给我一个选项来一次又一次地输入身份证号码。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    FILE *fr = fopen("file.csv", "r");
    char string[200], toFind[200], opt;
    int err = 0;
    char *line1 = NULL;

    do {
        printf("Enter ID Card number: ");
        scanf("%s", toFind);
        if (strlen(toFind) != 13) {
            break;
        }

        do {     // This loop is not working properly
            {
                err = 1;
                while (fgets(string, 200, fr)) {
                    line1 = strtok(string, "\n");
                    printf("%s\n", line1);
                    err = 0;
                }
            }
            printf("Repeat again [y/n]: ");
            scanf("%s", &opt); 
        } while (opt == 'y');
    } while(err);

    return 0;
}

In the first place, you must not use a %s directive to read input into a single char .首先,您不能使用%s指令将输入读入单个char That is for reading non-empty string input, and so it will always store at least two characters, the last being a string terminator.那是为了读取非空字符串输入,因此它将始终存储至少两个字符,最后一个是字符串终止符。 a single char does not have space for that, hence your program has undefined behavior.单个char没有空间,因此您的程序具有未定义的行为。 You could use %c instead, but be aware that the latter is among the few field directives that do not cause leading whitespace to be skipped.您可以改用%c ,但请注意,后者是少数不会导致跳过前导空格的字段指令之一。

Whenever I press y to enter again.每当我按 y 再次输入时。 It just repeats the Enter again [y/n] option again and again instead of starting from 2nd do-while loop.它只是一次又一次地重复 Enter [y/n] 选项,而不是从第二个 do-while 循环开始。

Even if we suppose that overwriting the bounds of of variable opt has no observable effect, you are mistaken: when the loop cycles to a new iteration, it starts again at the beginning of the loop body.即使我们假设覆盖变量opt的边界没有可观察到的效果,您也错了:当循环循环到新的迭代时,它会在循环体的开头再次开始。

But what will it do when it gets there?但是当它到达那里时会做什么? Consider that in order to reach the scanf in the first place, the program must first have reached and passed through the while(fgets(string, 200, fr)) loop.考虑到为了首先到达scanf ,程序必须首先到达并通过while(fgets(string, 200, fr))循环。 There is no goto or break in the body of that loop, so it will terminate only when fgets() returns a null pointer, indicating that it has reached the end of the file (most likely) or encountered an I/O error.该循环体中没有gotobreak ,因此只有在fgets()返回空指针时才会终止,表明它已到达文件末尾(最有可能)或遇到 I/O 错误。 When control cycles back around to that loop, nothing has changed with file fr .当控制循环回到那个循环时,文件fr没有任何改变。 It is still positioned at the end of the file (or is still in an error state), so there is every reason to expect that the next fgets() will again return a null pointer, so that the body of that loop is not executed even once.它仍然位于文件末尾(或仍处于错误状态),因此完全有理由期望下一个fgets()将再次返回空指针,从而不执行该循环的主体哪怕只有一次。

Perhaps you want to rewind(fr) before the fgets() loop so that you can read through the file again, but the point of that loop is unclear to me, so I'm not sure whether that really makes sense.也许您想在fgets()循环之前rewind(fr)以便您可以再次通读文件,但我不清楚该循环的重点,所以我不确定这是否真的有意义。

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

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