Hi, a while ago I posted here for help on an assignment for school, and the help I received was invaluable and helped me figure out how to complete my assignment!
This is my final assignment now, and it is programming a game of Hangman. It requires a function in addition to main, and uses strings.
I think I have the majority done, the rest is just finding and correcting mistakes (along with a few other questions that will likely arise later)..
I'm not very good with strings, so there may be some pretty stupid errors..
And for some reason it crashes after inputting the word... My compiler (and I) must be missing something..
Here's my code:
Code:
#include <stdio.h>#include <string.h>
int draw_guess(int length, char guess, char word[]) //the function draws the unguessed letters (dashes) and updates it when guessed correctly
{
//Draws the dashes
int i, startup = 0;
char display[i];
//Updates the dashes with the properly guessed letters
if(startup = 1)//only goes after the initial dashes are printed
for(i=0; i<length; i++)
{
if(word[i] == guess)
{
display[i] = guess;
printf("%s", display[i]);
}
else return 1; //if an incorrect letter is guessed, the function returns 1
}
else//this is just to print the initial dashes
{
for(i=0; i<length; i++)
{
display[i] = 'i';
printf("%s", display[i]);
}
}
startup = 1;
}
int main(void)
{
int length, wrong, position;
char guess;
char word[7], prevguess[17];
wrong = 0;
printf("Welcome to HANGMAN!\n");
printf("Please pick a word (2 to 6 letters): ");
scanf("%s", word);
length = strlen(word); //just takes the length of the word entered
printf("You are guessing a word that is %d letters long.\n", length);
while(wrong != 10) //keeps going for 10 incorrect guesses
{
draw_guess(length, guess, word); //SHOULD draw the initial dashes once, than afterwards begin printing the updated ones
printf("Make a guess (1 letter): ");
scanf("%c", guess);
position = strlen(prevguess); //this just sets up the string containing guesses
if(draw_guess(length, guess, word) == 1)//when the function returns 1, the guess was wrong
wrong = wrong + 1; //adds to the incorrect guess count
prevguess[position] = guess;//adds the guess into the string
printf("Your guesses: %s", prevguess);//shows the previous guesses
printf("Incorrect guesses: %d", wrong);//tells user how many times they have guessed wrong
}
return 0;
}