c - My Do-While loop doesn't seem to loop? -
please help, cant seem do-while loop loop when ask user input continue using scanf , printf. wipe_buffer? can't seem right on , need finish loop , should functional. new coding , website please not harsh. please , thanks.
#include <stdio.h> #include <stdlib.h> void wipe_buffer(void); int main(int argc, char* argv[]) { char play1; char play2; char cont; do{ printf("player 1 pick rock, paper, or scissors\n"); scanf(" %c", &play1); wipe_buffer(); printf("player 2 pick rock, paper, or scissors\n"); scanf(" %c", &play2); wipe_buffer(); switch(play1) { case 'r': if(play2 == 'p' || play2 == 'p') { printf("paper covers rock\n"); printf("player 2 wins\n"); } if(play2 == 's' || play2 == 's') { printf("rock breaks scissors\n"); printf("player 1 wins\n"); } if(play2 == 'r' || play2 == 'r') { printf("draw, nobody wins\n"); } break; case 'r': if(play2 == 'p' || play2 == 'p') { printf("paper covers rock\n"); printf("player 2 wins\n"); } if(play2 == 's' || play2 == 's') { printf("rock breaks scissors\n"); printf("player 1 wins\n"); } if(play2 == 'r' || play2 == 'r') { printf("draw, nobody wins\n"); } break; case 'p': if(play2 == 'p' || play2 == 'p') { printf("draw, nobody wins\n"); } if(play2 == 's' || play2 == 's') { printf("scissors cuts paper\n"); printf("player 2 wins\n"); } if(play2 == 'r' || play2 == 'r') { printf("paper covers rock\n"); } break; case 'p': if(play2 == 'p' || play2 == 'p') { printf("draw, nobody wins\n"); } if(play2 == 's' || play2 == 's') { printf("scissors cuts paper\n"); printf("player 2 wins\n"); } if(play2 == 'r' || play2 == 'r') { printf("paper covers rock\n"); } break; case 's': if(play2 == 'p' || play2 == 'p') { printf("scissors cuts paper\n"); printf("player 1 wins\n"); } if(play2 == 's' || play2 == 's') { printf("draw, nobody wins\n"); } if(play2 == 'r' || play2 == 'r') { printf("rock breaks scissors\n"); printf("player 2 wins\n"); } break; case 's': if(play2 == 'p' || play2 == 'p') { printf("scissors cuts paper\n"); printf("player 1 wins\n"); } if(play2 == 's' || play2 == 's') { printf("draw, nobody wins\n"); } if(play2 == 'r' || play2 == 'r') { printf("rock breaks scissors\n"); printf("player 2 wins\n"); } break; } printf("do wish continue?\n"); scanf(" &c", &cont); wipe_buffer(); }while(cont == 'y' || cont == 'y'); } void wipe_buffer(void) { char t; scanf("%c", &t); while(t != '\n' ) { scanf("%c", &t); } return; }
in section:
printf("do wish continue?\n"); scanf(" &c", &cont);
"&c"
should "%c"
note when compiled gcc, got warning addressed exact problem, make sure read warnings.
Comments
Post a Comment