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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>

#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<len; i++)
    mask[i]='_';
for(i=len; i<20; i++)
    mask[i]='\0';

    while( true ){
        printf("\nThe word:  %s ", mask);
        printf("\nGuess (a-z): ");
        scanf(" %c", &guess);

        clear_stdin();
        if(guess==ABORT_CH)
            break;
        count++;

p=0;
for(i=0; i<len; i++){
    if(word[i]==guess)
        {mask[i]=guess;
            p=1;}
    else continue;}


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

p=0;
for(i=0; i<len; i++){
    if(word[i]!=mask[i])
        p=1;}


if(p==0)
        break;
else if(wrong==6)
        break;
else   continue;

}


if(guess==ABORT_CH)
    result=ABORTED;
else if(wrong!=6)
    result=WIN;
else
    result=LOOSE;


    if( result == WIN ){
      printf("you won!\n ");
    }else if( result == LOOSE ){
      printf("Lost, the word was: %s\n ", word);
    }else {
      printf("cancelled\n ");
      break;
    }

    ch = get_user_input("  Do you want to play again (y/n)?): ");

if(c=='n'  ||  ch=='N')
   break;

}

  return 0;
}


void clear_stdin()
{
while(getc(stdin) == '\n')
break;
}




int draw_hangman(int wrong){

 char fig[6][20];
 int i;

 strcpy(fig[0], "\t\t\t\t\t\t    _______ \n");
 strcpy(fig[1], "\t\t\t\t\t\t       | \n");
 strcpy(fig[2], "\t\t\t\t\t\t       0 \n");
 strcpy(fig[3], "\t\t\t\t\t\t    // | \\\\ \n");
 strcpy(fig[4], "\t\t\t\t\t\t       | \n");
 strcpy(fig[5], "\t\t\t\t\t\t     // \\\\ \n");

for(i=0; i<wrong; i++)
    printf("%s ",fig[i]);

    return wrong;

}



char get_user_input(char *str){
	char tkn='0';


    printf("%s ", str);

        scanf(" %c", &tkn);
	return tkn;
}
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.
closed account (o3hC5Di1)
Hi there,

You may want to try changing:

82
83
84
if(p==0)
    {wrong++;
    draw_hangman(wrong);}


To:

82
83
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:

1
2
3
4
5
6
7
8
9
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.