c - Using getchar() to exit a loop when user hits 'Enter'? -
i have read lot of other stack overflow topics on still having lot of trouble. have tried use do/while loop nested if statement. can't last if stop printing 'cards' when the 'player' hits 'enter'. ether getchar() holds if statement print if user continually presses enter (which not close intending) or continues print after pausing.
void printdeck(card *deck, const char *faces[], const char *suits[], file *fp) { int loop, t; // print deck 1 card @ time fp = fopen("cardsprinted.txt", "w+"); t = ftell(fp); char c; { c = getchar(); (loop = 0; loop < 52; loop++) { // print face , suit of card stdout file printf("%s of %s\n",faces[deck[loop].face],suits[deck[loop].suit]); fprintf(fp,"%s of %s\n",faces[deck[loop].face],suits[deck[loop].suit]); fseek(fp, t, seek_set); // finds beginning of file fseek(fp,t,seek_end);// finds end of file printing // loop pause 3seconds before printing next card. if ((loop % 1) == 0 && loop != 0) { sleep(3); } } } while (c != '\n' && c != eof); fclose(fp); }
output examples:
./slapjack queen of spades 9 of spades jack of hearts
this hit enter , want loop stop printing.
three of hearts queen of hearts 6 of spades 7 of diamonds ^c
but doesn't work...
i have tried not using getchar(). thought maybe scanf() easier still no luck.
use while statement.
set control var true
before entering, test getchar if detected set control var false
enter while loop
psudocode below:
controlvar = ! getchar() while (controlvar) { test end condition or getchar set controlvar false }
Comments
Post a Comment