HII everyone !
i write a programme's code which can choose a word randomly from a txt file
then the user try to find a characater from the word if the typed character is in the word all the other similar Character will appear
here is my code just before the probleme is when i type a character the programme displays 2 times the same thing like this :
you still have 10 turn :
what is the secret word :
******
propose a character :A
you still have 9 turn :
what is the secret word :
******
propose a character :
you still have 8 turn :
what is the secret word :
******
propose a character :
the code :
Code:
#include <stdio.h> // include appropriate headers
#include <stdlib.h>
#include <string.h>
// use define's for constants
#define MAXWORD 100
#define FILENAME "test.txt"
int main(void) { // use void if you aren't using main's optional input parameters
char word[MAXWORD];
char wordtmp[MAXWORD];
char carac=0;
char t[MAXWORD];
int pos = 1,counter=10,i=0,cpt=0;
FILE* file = NULL;
file = fopen(FILENAME, "r"); // use "r" not "r+" if you're just reading
if (file == NULL) // deal with file error right here
{
fprintf(stderr, "Can't open %s.\n", FILENAME);
return EXIT_FAILURE;
}
// use fgets return value to control the loop
while (fgets(word, MAXWORD, file))
{
if (pos == 3)
{
strcpy(wordtmp,word);
break; // exit the loop when you find the word
}
++pos;
}
fclose(file);
while(wordtmp[i]!='\0')
{
i++;
}
for(cpt=0;cpt<MAXWORD;cpt++)
{
t[cpt]='*';
}
do
{
printf("you still have %d turn : \n",counter);
printf("what is the secret word : \n");
for(cpt=0;cpt<i;cpt++)
{
if(carac==wordtmp[cpt])
{
t[cpt]=carac;
counter++;
printf("%c",t[cpt]);
}
else
{
printf("%c",t[cpt]);
}
}
printf("\n");
printf("propose a character :");
scanf("%c",&carac);
printf("\n \n");
counter--;
}while(counter>0);
return EXIT_SUCCESS;
}