简体   繁体   中英

How to make the do while loop work properly?

In this code, the second do while loop is not working properly. Whenever I press y to enter again. It just repeats the Enter again [y/n] option again and again instead of starting from 2nd 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 . 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. 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.

Whenever I press y to enter again. It just repeats the Enter again [y/n] option again and again instead of starting from 2nd do-while loop.

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.

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. 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. When control cycles back around to that loop, nothing has changed with file 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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