【发布时间】:2016-11-13 05:33:05
【问题描述】:
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 中的最后 2 行时,我调用了 pause 函数,提示用户按 enter,并且在用户没有按 enter 之前不要在程序中前进。出于某种原因,当他们按下回车键时,暂停函数的提示和 printf 语句中的字符串打印在同一行。由于暂停等待用户按Enter键,它不应该在单独的行上打印吗?它在最后一次运行之前每隔一次执行一次,但为什么在最后一次调用函数 pause 时执行它?提前致谢。
输出如下:"Press <ENTER> to continue...dspItem, Linear with low value and taxed:"
【问题讨论】:
-
尝试在您的
pause中的printf之后添加fflush(stdout)。printf默认为行缓冲。所以它不会输出到终端直到换行或明确的flush。 -
在询问运行时问题时,(除其他外)发布干净编译的代码。发布的代码无法编译,缺少函数原型,缺少`#include
语句等。
标签: c function output console-application