Well I did ask about static variables earlier but no response.. But I added it in now, thanks. Now you said that each possible result needs there to be a return value right? The only return value I am going to end up using in main is when the guess is false. So what happens with the other values?
Edit: After further consideration I think I can probably find another way to keep track of when it is wrong and just make the function void..
Edit 2: Here it is in the entire function. I am aware that the "wrong" bit is very likely done incorrectly though, im trying to figure that out.
Code:
#include <stdio.h>#include <string.h>
void 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, wrong;
static int startup = 0;
char display[7];
//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);
}
else wrong = 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=i+1)
{
display[i] = 'i';
printf("%s", display);
}
}
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
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;
}
The overall program still crashes however!