首页 > 解决方案 > 即使有scanf,它也跳过了行并且不要求输入

问题描述

这是代码。如果我问了明显的问题,我很抱歉

int main()
{
    int stat;
    char v_id[100], c_id[100], line[BUFFER_SIZE], tos[100], cust_stat[100], n_daysasd[100], tot_fee[100];;
    printf("Enter Visitor ID: ");
    scanf("%s", &v_id);
    printf("Enter Customer ID: ");
    scanf("%s", &c_id);
    FILE* fptr2;
    FILE* fptr;
    FILE* ftemp;
    fptr = fopen(v_id, "r");
    fptr2 = fopen("servicestest.txt", "r");
    ftemp = fopen("replacetemp.txt", "w");
    while (fgets(line, BUFFER_SIZE, fptr))
    {
        printf("%s\n", line);
    }

    printf("\nEnter the information accordingly\n");

    rewind(fptr);
    stat = 0;

    while (stat == 0)
    {
        printf("TOS: ");
        scanf("%s", &tos);
        getchar();
        while (fgets(line, BUFFER_SIZE, fptr) != 0)
        {
            if (strstr(line, tos))
            {
                stat = 1;
            }
        }
    }

    rewind(fptr);
    stat = 0;

    while (stat == 0)
    {
        printf("Customer Status: ");
        scanf("%s", &cust_stat);
        getchar();
        while (fgets(line, BUFFER_SIZE, fptr) != 0)
        {
            printf("it did enter here");
            if (strstr(line, cust_stat))
            {
                stat = 1;
            }
        }
    }

    rewind(fptr);
    stat = 0;

    while (stat == 0)
    {
        printf("Needed days: ");
        scanf("%s", &n_daysasd);
        while (fgets(line, BUFFER_SIZE, fptr))
        {
            if (strstr(line, n_daysasd))
            {
                stat = 1;
            }
        }
    }

    rewind(fptr);
    stat = 0;

    while (stat == 0)
    {
        printf("Total Fee: ");
        scanf("%s", &tot_fee);
        while (fgets(line, BUFFER_SIZE, fptr))
        {
            if (strstr(line, tot_fee))
            {
                stat = 1;
            }
        }
    }

    while (fgets(line, BUFFER_SIZE, fptr2))
    {
        if (strstr(line, v_id))
        {
            fprintf(ftemp, "Visit ID: %s, Customer ID : %s, TOS : %s, Customer Status : %s, Needed Days : %s, Total Fee : %s, Payment : Not Made", v_id, c_id, tos, cust_stat, n_daysasd, tot_fee);
        }
        else
        {
            fputs(line, ftemp);
        }
    }

    fclose(fptr);
    fclose(fptr2);
    fclose(ftemp);

    remove("servicestest.txt");

    rename("replacetemp.txt", "servicestest.txt");

总结就是这里

while (stat == 0)
        {
            printf("TOS: ");
            scanf("%s", &tos);
            getchar();
            while (fgets(line, BUFFER_SIZE, fptr) != 0)
            {
                if (strstr(line, tos))
                {
                    stat = 1;
                }
            }
        }

这是示例输入

在此处输入图像描述

我尝试随机输入,但随后正确输入了它应该做的事情,但由于某种原因,它显示TOS: TOS: TOS: TOS:了四次,我不知道为什么。

这也是另一个示例输入

在此处输入图像描述

它应该要求输入,但它没有,而是跳到total fee:要求输入,甚至跳过了需要的日子。

我确实倒带了fptr,那么错误在哪里?为什么它不等待输入?

标签: c

解决方案


推荐阅读