T
tigrfire
So I'm trying to write a hangman game and the output is coming out a
little strange. Here's my code thus far:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int hangman()
{
char input;
char temp[10];
char word[][20] = {"literature","scholar", "enormous", "influence",
"publication", "pioneer", "reshape", "catalyst", "leader", "member",
"final", "phonograph", "executed",
"oldest", "people", "requiring", "screwdriver", "buckshot",
"different", "striking"};
int k = rand()%20;
int i = 0;
int r = 0;
char b[]= "____________";
int length = strlen(word[k]);
int turn = length, dummy = 0, decide = 0, z = 0;
for(i=0; i < length; i++)
{
printf("%c", b);
}
while(turn != 0)
{
if (strcmp(temp,word[k]) == 0)
{
printf("You have guessed");
}
printf("\nTurns left: ");
printf("%d", turn);
printf("\nEnter a letter to guess: ");
scanf("%c", &input);
for(r = 0; r < length; r++)
{
if(input == word[k][r] && b[r] == '_')
{
decide = 1;
turn = length - dummy;
}
else
{
decide = 0;
}
}
if(decide == 0)
{
dummy++;
turn = length - dummy;
}
for(r = 0; r < length; r++)
{
if(input == word[k][r])
{
b[r] = word[k][r];
temp[r] = word[k][r];
temp[length] = '\0';
printf("%c", b[r]);
}
z++;
}
}
if(strcmp(temp, word[k]) != 0)
{
printf("Sorry, no more turns left. The secret word was %s.",
word[k]);
}
else
{
printf("Congratulations!\nYou guessed the secret word: %s",
word[k]);
}
return 0;
}
int main()
{
char response;
do
{
hangman();
printf("Do you want to continue (y or n): ");
scanf("%c", &response);
}
while(response == 'y');
return 0;
}
This produces an output similar to this:
_________
Turns left: 9
Enter a letter to guess: b
Turns left: 8
Enter a letter to guess:
Turns left: 7
Enter a letter to guess: e
ee
Turns left: 7
Enter a letter to guess:
Turns left: 6
Enter a letter to guess:
(note: the output would have continued but I suspended it)
Here's my problem, I am trying to get the program to read the strlen
and output one * for each 'hidden' letter of the word, that doesn't
seem so bad. My real problem is that I want those asteriscks to stay
there and get replaced by the correct letter when guessed. Also, I
don't really know why it subtracts two turns away each time or
sometimes none at all. Any help?
little strange. Here's my code thus far:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int hangman()
{
char input;
char temp[10];
char word[][20] = {"literature","scholar", "enormous", "influence",
"publication", "pioneer", "reshape", "catalyst", "leader", "member",
"final", "phonograph", "executed",
"oldest", "people", "requiring", "screwdriver", "buckshot",
"different", "striking"};
int k = rand()%20;
int i = 0;
int r = 0;
char b[]= "____________";
int length = strlen(word[k]);
int turn = length, dummy = 0, decide = 0, z = 0;
for(i=0; i < length; i++)
{
printf("%c", b);
}
while(turn != 0)
{
if (strcmp(temp,word[k]) == 0)
{
printf("You have guessed");
}
printf("\nTurns left: ");
printf("%d", turn);
printf("\nEnter a letter to guess: ");
scanf("%c", &input);
for(r = 0; r < length; r++)
{
if(input == word[k][r] && b[r] == '_')
{
decide = 1;
turn = length - dummy;
}
else
{
decide = 0;
}
}
if(decide == 0)
{
dummy++;
turn = length - dummy;
}
for(r = 0; r < length; r++)
{
if(input == word[k][r])
{
b[r] = word[k][r];
temp[r] = word[k][r];
temp[length] = '\0';
printf("%c", b[r]);
}
z++;
}
}
if(strcmp(temp, word[k]) != 0)
{
printf("Sorry, no more turns left. The secret word was %s.",
word[k]);
}
else
{
printf("Congratulations!\nYou guessed the secret word: %s",
word[k]);
}
return 0;
}
int main()
{
char response;
do
{
hangman();
printf("Do you want to continue (y or n): ");
scanf("%c", &response);
}
while(response == 'y');
return 0;
}
This produces an output similar to this:
_________
Turns left: 9
Enter a letter to guess: b
Turns left: 8
Enter a letter to guess:
Turns left: 7
Enter a letter to guess: e
ee
Turns left: 7
Enter a letter to guess:
Turns left: 6
Enter a letter to guess:
(note: the output would have continued but I suspended it)
Here's my problem, I am trying to get the program to read the strlen
and output one * for each 'hidden' letter of the word, that doesn't
seem so bad. My real problem is that I want those asteriscks to stay
there and get replaced by the correct letter when guessed. Also, I
don't really know why it subtracts two turns away each time or
sometimes none at all. Any help?