当我清除输入缓冲区时,为什么它不会进入新行?

时间:2016-11-13 05:33:06

标签: c function output console-application

void clrKyb(void) {   
    char input = ' ';
    do {
        scanf("%c", &input);
    } while (input != '\n');
}

void pause(void) {
    //Pause the program as until the user presses enter
    printf("Press <ENTER> to continue...");
    clrKyb();
}

int main() {
    struct Item I[21] = {
        { 4.4, 275, 0, 10, 2, "Royal Apples" },
        { 5.99, 386, 0, 20, 4, "Watermelon" },
        { 3.99, 240, 0, 30, 5, "Blueberries" },
        { 10.56, 916, 0, 20, 3, "Seedless Grapes" },
        { 2.5, 385, 0, 5, 2, "Pomegranate" },
        { 0.44, 495, 0, 100, 30, "Banana" },
        { 0.5, 316, 0, 123, 10, "Kiwifruit" },
        { 4.49, 355, 1, 20, 5, "Chicken Alfredo" },
        { 5.49, 846, 1, 3, 5, "Veal Parmigiana" },
        { 5.29, 359, 1, 40, 5, "Beffsteak Pie" },
        { 4.79, 127, 1, 30, 3, "Curry Checken" },
        { 16.99, 238, 1, 10, 2, "Tide Detergent" },
        { 10.49, 324, 1, 40, 5, "Tide Liq. Pods" },
        { 10.99, 491, 1, 50, 5, "Tide Powder Det." },
        { 3.69, 538, 1, 1, 5, "Lays Chips S&V" },
        { 3.29, 649, 1, 15, 5, "Joe Org Chips" },
        { 1.79, 731, 1, 100, 10, "Allen's Apple Juice" },
        { 6.69, 984, 1, 30, 3, "Coke 24 Pack" },
        { 7.29, 350, 1, 50, 5, "Nestea 24 Pack" },
        { 6.49, 835, 1, 20, 2, "7up 24 pack" }
    };
    double val;
    int ival;
    int searchIndex;
    val = totalAfterTax(I[0]);
    printf("totalAfterTax:\n"
           "    yours=%lf\n"
           "program's=44.000000\n", val);
    val = totalAfterTax(I[7]);
    printf("totalAfterTax:\n"
           "    yours=%lf\n"
           "program's=101.474000\n", val);
    ival = isLowQty(I[0]);
    printf("isLowQty:\n"
           "    yours=%d\n"
           "program's=0\n",ival);
    ival = isLowQty(I[14]);
    printf("isLowQty:\n"
           "    yours=%d\n"
           "program's=1\n",ival);
    pause();

    printf("itemEntry, enter the following values:\n");
    printf("        SKU: 999\n"
           "       Name: Red Apples\n"
           "      Price: 4.54\n"
           "   Quantity: 50\n"
           "Minimum Qty: 5\n"
           "   Is Taxed: n\n");
    printf("Enter the values:\n");
    I[20] = itemEntry(999);
    printf("dspItem, Linear:\nYours: ");
    dspItem(I[20], LINEAR);
    printf(" Prog: |999|Red Apples          |    4.54|   No|  50 |   5 |       227.00|\n");
    printf("dspItem, Form:\nYours:\n");
    dspItem(I[20], FORM);
    printf("Programs: \n");
    printf("        SKU: 999\n"
           "       Name: Red Apples\n"
           "      Price: 4.54\n"
           "   Quantity: 50\n"
           "Minimum Qty: 5\n"
           "   Is Taxed: No\n");
    I[20].quantity = 2;
    I[20].isTaxed = 1;
    pause();

    printf("dspItem, Linear with low value and taxed:\nYours: ");

    return 0;
}

当我尝试执行main中的最后两行时,我调用pause函数,提示用户按Enter键,并且在用户未按Enter键之前不要在程序中前进。出于某种原因,当他们按回车键时,来自pause函数的提示和来自printf语句的字符串将在同一行上打印。因为暂停等待用户点击输入,所以它不应该在单独的行上打印吗?它在最后一次运行之前每隔一段时间执行一次,但为什么它会在最后一次调用函数暂停时执行呢?提前谢谢。

输出如下:"Press <ENTER> to continue...dspItem, Linear with low value and taxed:"

1 个答案:

答案 0 :(得分:2)

出于某种原因,在您的系统上,stdout在从stdin请求输入时不会刷新。您可以通过拨打fflush()强制执行此操作:

void pause(void) {
    //Pause the program as until the user presses enter
    printf("Press <ENTER> to continue...");
    fflush(stdout);
    clrKyb();
}

请注意,如果在读取换行符之前在clrKyb()中到达文件末尾,则函数stdin将运行无限循环。你应该改用它:

void clrKyb(void) {
    int c;
    while ((c = getchar()) != EOF && c != '\n')
        continue;  
}