in the for loop, and in the if statements, for some reason it goes through both if statements even though they are the opposite of each other and i dont know why it is doing it. I have tried different ways and it still comes out the same way.
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char secretword[20] = {};
char alphabet[28] = {"abcdefghijklmnopqrstuvwxyz "};
char guess[2] = {};
int i = 0, k = 0;
int seclength = 0, alphlength = 0;
int GuessCtr = 6;
printf("You Get six chances to guess all of the letters in a phrase\n");
printf("Enter the secret word/phrase: ");
scanf("%s", &secretword);
seclength = strlen(secretword);
alphlength = strlen(alphabet);
while(GuessCtr != 0)
{
printf("Past guesses: ");
for(i = 0; i < alphlength; i++)
{
printf("%c", alphabet[i]);
}
printf("\n");
printf("Guess a character: ");
scanf("%s", &guess);
printf("\n");
for(i = 0; i < seclength; i++)
{
if(secretword[i] == guess[0])
{
secretword[i] = '*';
}
/*else
{
GuessCtr--;
printf("You missed - you have %d wrong guesses left!", GuessCtr);
}*/
if(secretword[i] != guess[0])
{
GuessCtr--;
printf("You missed - you have %d wrong guesses left!", GuessCtr);
}
}
for(i = 0; i < seclength; i++)
{
printf("%c", secretword[i]);
}
printf("\n");
for(i = 0; i < alphlength; i++)
{
if(alphabet[i] == guess[0])
{
alphabet[i] = '*';
}
}
}
printf("You suck!");
return 0;
}
Because the body of the first
ifstatement changes the variable that it tested, and the second one will test again. When the secondifis evaluated, the variable has a different value than it did the first time.To fix this, you need to use
else, so the condition is “remembered” from the first time: