c - Why isn't it going to a new line when I clear my input buffer? -
void clrkyb(void) { char input = ' '; { scanf("%c", &input); } while (input != '\n'); } void pause(void) { //pause program until user presses enter printf("press <enter> 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 following values:\n"); printf(" sku: 999\n" " name: red apples\n" " price: 4.54\n" " quantity: 50\n" "minimum qty: 5\n" " taxed: n\n"); printf("enter 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" " taxed: no\n"); i[20].quantity = 2; i[20].istaxed = 1; pause(); printf("dspitem, linear low value , taxed:\nyours: "); return 0; } when try execute last 2 lines in main, call pause function, prompt user press enter, , not progress forward in program until user not press enter. reason, when press enter, prompt pause function , string printf statement printing on same line. isn't supposed print on separate line since pause waits user hit enter? every other time before last time runs, why doing on last time function pause called? in advance.
output looks like: "press <enter> continue...dspitem, linear low value , taxed:"
for reason, on system, stdout not flushed when input requested stdin. can force call fflush():
void pause(void) { //pause program until user presses enter printf("press <enter> continue..."); fflush(stdout); clrkyb(); } note function clrkyb() run infinite loop if end of file reached in stdin before newline read. should use instead:
void clrkyb(void) { int c; while ((c = getchar()) != eof && c != '\n') continue; }
Comments
Post a Comment