### help with flushing stdin.(i think)

Hi guys.
I have done this hangman homework but i have 2 problems.
The first is that when I get to `"Do you want to play again?"` , and if my answer is `yes`, sometimes it restarts and sometimes it ends.
Can it be the stdin? Then how do I flush it correctly, and where?

The second problem is when the hangman figure gets drawn, sometimes it draws it correctly, and sometimes it misses one part.
Im suspecting its a similar problem here.

Here is my whole code:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167`` ``````#include #include #include #include #define MAX_WORD_LEN 20 const int MAX_GUESS = 6; const char ABORT_CH = '0'; const int LOOSE = 0; const int WIN = 1; const int ABORTED = 2; char get_user_input(char *str); int draw_hangman(int fel); void clear_stdin(); int main() { char word[MAX_WORD_LEN]; /* Word to guess (from file) */ char mask[MAX_WORD_LEN]; /* Masked word (shown on screen) */ char guess; char ch; /* User selection */ int count; /* Number of guesses */ int len; int result; /* Winner */ int p,i=0,wrong, nrofwords=0, random; srand( time(0) ); while( true){ wrong=0; FILE *file; fil=fopen("hangman.txt", "r"); while(!feof(file)) {fscanf(file, " %s", word); nrofwords++;} fclose(file); random = rand()%nrofwords+1; file=fopen("hangman.txt", "r"); do{fscanf(file, " %s", word); i++;}while(i!=random); fclose(file); len = strlen(word); printf( "\nWelcome "); for(i=0; i
Last edited on
`if(c=='n' || ch=='N')`

Should be: `if(ch=='n' || ch=='N')`

HTH,
Aceix.
Thank you, but the problem is still there even if i write that correct. Any idea?
Missing `i=0;` at line number 34.
Thanks that solved the first problem!
The hangman problem is still there though.
Hi there,

You may want to try changing:

 ``828384`` ``````if(p==0) {wrong++; draw_hangman(wrong);}``````

To:

 ``8283`` ``````if(p==0) { draw_hangman(wrong++);}``````

Hope that helps.

All the best,
NwN
Nope, that makes it draw the figure too late.
The figure gets drawn in perfect order, but its just that sometimes it skips one part. But the program still works 100% even though it might skip one part, because the next wrong answer draws the missing part and the part its supposed to draw.

Example:

 ``123456789`` ``````Wrong 1: _______ Wrong 2: _______ (skips the neck) Wrong 3: _______ (but here it draws the neck and the head.) | 0 ``````
Last edited on
Topic archived. No new replies allowed.