gpt4 book ai didi

c - 为什么在我清除输入缓冲区时它不换行?

转载 作者:太空宇宙 更新时间:2023-11-04 02:33:21 25 4
gpt4 key购买 nike

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;
}

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

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

最佳答案

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

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

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

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

关于c - 为什么在我清除输入缓冲区时它不换行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40570910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com