#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Help with Hangman in C please


    Hi, I am a freshman in college and need some guidance with my hangman in C program. I have this much finished.. I am using xcode and am not sure where to go next everything seems to work so far. Any help appreciated.

    Thanks :)

    #define _CRT_SECURE_NO_DEPRECATE
    #include <stdio.h>
    #include <string.h>
    #define SIZE 20
    #define MAX 6

    void Instructions ( );
    int Compare( char guessedLetters[ ], int i, char word[ ], int incorrect[ ]);
    void Asterisks(char *word, char *wordInProgress);
    void LettersGuessed ( char guessedLetters[ ] );
    void GetGuess( char guessedLetters[ ]);
    void ReplaceAsterisk(char guessedLetters[ ], char wordInProgress[ ], int i, char word[ ]);

    int main( )
    {

    FILE *infile;
    char word[ SIZE ];
    char wordInProgress [ SIZE ];
    int i;
    char guess;
    char guessedLetters[SIZE];
    int numGuesses[MAX];
    char again= 'Y';
    int incorrect[MAX];
    int correct;

    infile = fopen("words.txt", "r");

    fscanf( infile, " %s", word);

    Instructions( );





    if (again=='Y' || again=='y')
    {
    printf("Guess this word: \n\n");
    Asterisks(word, wordInProgress);
    LettersGuessed ( guessedLetters );
    GetGuess( guessedLetters);
    Compare( guessedLetters, i, word, incorrect);
    ReplaceAsterisk( guessedLetters, wordInProgress, i, word);
    }
    if (again=='Q' || again=='q')
    {
    printf("Thanks for playing!");
    }






    printf(" %s", wordInProgress);



    fclose(infile);





    return 0;
    }





    void Instructions ( )
    {
    printf("WELCOME TO HANGMAN!\n\n");
    printf("Please read the following instructions before you play.\n\n");
    printf("-You will be presented with a word to be guessed\n");
    printf("-Guess letters one at a time\n");
    printf("-You can have up to six incorrect guesses\n");
    printf("-The game will be OVER when you have guessed all the letters in the word or when you have guessed incorrectly SIX times.\n\n");
    printf("HAVE FUN!\n\n");
    printf("--------------------\n\n");
    }









    int Compare( char guessedLetters[ ], int i, char word[ ], int incorrect[ ])
    {
    for (i=0; i<SIZE; i++)
    {
    if (guessedLetters[i]==word[i])
    {
    printf("\nYou guessed a letter!");
    return 0;
    }
    }

    if (guessedLetters[i]!=word[i])
    {
    printf("\nThat letter is not in the word! Try again.");
    incorrect[i]++;
    }

    return 0;
    }










    void Asterisks(char *word, char *wordInProgress)
    {
    int length = 0;
    int i;
    length=strlen(word);//counts the amount of characters in the word
    for (i = 0; i < length; i++)
    {
    wordInProgress[i] = '*';//makes the asterisks array
    }
    printf(" %s", wordInProgress);
    }











    void LettersGuessed ( char guessedLetters[ ] )
    {
    printf("\n\n");
    printf("Letters guessed: %s", guessedLetters);
    }









    void GetGuess( char guessedLetters[ ])
    {
    printf("\nEnter a guess: ");
    scanf(" %s", guessedLetters);
    }









    void ReplaceAsterisk(char guessedLetters[ ], char wordInProgress[ ], int i, char word[ ])
    {
    for (i = 0; i < SIZE; i++)
    {
    if (word[i] == *guessedLetters)
    {
    wordInProgress[i] = *guessedLetters;
    break;
    }
    }
    }








    char Again ( char again )
    {
    printf("Would you like to play another game?");
    scanf(" %c", &again);
    return again;
    }





    void NumGuesses( int numGuesses )
    {

    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    "xcode"? That macro you're defining looks like Visual Studio instead.

    Please use code tags to preserve your code's indenting. HTML strips that all out, so you need to use code tags to keep that from happening. It's very simple:

    [code] copy and paste your formatted code here [/code]

    Here is what your code looks like with code tags -- original formatting retrieved via the Reply button:
    Code:
    #define _CRT_SECURE_NO_DEPRECATE
    #include <stdio.h>
    #include <string.h>
    #define SIZE 20
    #define MAX 6
    
    void Instructions ( );
    int Compare(	char guessedLetters[ ], int i, char word[ ], int incorrect[ ]);
    void Asterisks(char *word, char *wordInProgress);
    void LettersGuessed ( char guessedLetters[ ] );
    void GetGuess( char guessedLetters[ ]);
    void ReplaceAsterisk(char guessedLetters[ ], char wordInProgress[ ], int i, char word[ ]);
    
    int main( )
    {
    
    	FILE *infile;
    	char word[ SIZE ];
    	char wordInProgress [ SIZE ];
    	int i;
    	char guess;
    	char guessedLetters[SIZE];
    	int numGuesses[MAX];
    	char again= 'Y';
    	int incorrect[MAX];
    	int correct;
    	
    	infile = fopen("words.txt", "r");
    	
    	fscanf( infile, " %s", word);
    	
    	Instructions( );
    	
    	
    	
    	
    	
    	if (again=='Y' || again=='y')
    	{
    		printf("Guess this word:  \n\n");
    		Asterisks(word, wordInProgress);
    		LettersGuessed ( guessedLetters );
    		GetGuess( guessedLetters);
    		Compare( guessedLetters, i, word, incorrect);
    		ReplaceAsterisk( guessedLetters, wordInProgress, i, word);
    	}
    	if (again=='Q' || again=='q')
    	{
    		printf("Thanks for playing!");
    	}
    	
    	
    	
    	
    	
    	
    	printf(" %s", wordInProgress);
    	
    	
    	
    	fclose(infile);
    	
    	
    	
    	
    	
    		return 0;
    }
    
    
    
    
    
    void Instructions ( )
    {
    	printf("WELCOME TO HANGMAN!\n\n");
    	printf("Please read the following instructions before you play.\n\n");
    	printf("-You will be presented with a word to be guessed\n");
    	printf("-Guess letters one at a time\n");
    	printf("-You can have up to six incorrect guesses\n");
    	printf("-The game will be OVER when you have guessed all the letters in the word or when you have guessed incorrectly SIX times.\n\n");
    	printf("HAVE FUN!\n\n");
    	printf("--------------------\n\n");
    }
    
    
    
    
    
    
    
    
    
    int Compare(	char guessedLetters[ ], int i, char word[ ], int incorrect[ ])
    {
    	for (i=0; i<SIZE; i++)
    	{
    		if (guessedLetters[i]==word[i])
    		{
    			printf("\nYou guessed a letter!");
    			return 0;
    		}
    	}
    
    	if (guessedLetters[i]!=word[i])
    	{
    		printf("\nThat letter is not in the word! Try again.");
    		incorrect[i]++;
    	}
    	
    	return 0;
    }
    
    
    
    
    
    
    
    
    
    
    void Asterisks(char *word, char *wordInProgress)
    {
    	int length = 0;
    	int i;
    	length=strlen(word);//counts the amount of characters in the word
    	for (i = 0; i < length; i++)
    	{
    		wordInProgress[i] = '*';//makes the asterisks array
    	}
    	printf(" %s", wordInProgress);
    }
    
    
    
    
    
    
    
    
    
    
    
    void LettersGuessed ( char guessedLetters[ ] )
    {
    	printf("\n\n");
    	printf("Letters guessed:  %s", guessedLetters);
    }
    
    
    
    
    
    
    
    
    
    void GetGuess( char guessedLetters[ ])
    {
    	printf("\nEnter a guess:  ");
    	scanf(" %s", guessedLetters);
    }
    
    
    
    
    
    
    
    
    
    void ReplaceAsterisk(char guessedLetters[ ], char wordInProgress[ ], int i, char word[ ])
    {
    	for (i = 0; i < SIZE; i++) 
    	{ 
    	if (word[i] == *guessedLetters) 
    	{ 
    	wordInProgress[i] = *guessedLetters; 
    	break; 
    	} 
    	}
    }
    
    
    
    
    
    
    
    
    char Again ( char again )
    {
    	printf("Would you like to play another game?");
    	scanf(" %c", &again);
    	return again;
    }
    
    
    
    
    
    void NumGuesses( int numGuesses )
    {
    	
    }
    There, isn't that much better? Without code tags, your code is just plain unreadable.

    It's your design, so you know far better than we do what you intend to do and what still needs to be done.

    Though in main(), shouldn't that be some way to change the value of again? And all that Compare ever returns is 0. It looks like you have a lot of code stubs waiting to be filled in, a good general approach to developing a program, but you do eventually need to fill in those stubs.

    Also, you need to test the return value of fopen in order to tell whether it succeeded or failed. You must never try to use a file that did not open successfully.
    Last edited by dwise1_aol; November 16th, 2013 at 12:16 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Where to go next


    Originally Posted by dwise1_aol
    "xcode"? That macro you're defining looks like Visual Studio instead.

    Please use code tags to preserve your code's indenting. HTML strips that all out, so you need to use code tags to keep that from happening. It's very simple:

    [code] copy and paste your formatted code here [/code]

    Here is what your code looks like with code tags -- original formatting retrieved via the Reply button:
    Code:
    #define _CRT_SECURE_NO_DEPRECATE
    #include <stdio.h>
    #include <string.h>
    #define SIZE 20
    #define MAX 6
    
    void Instructions ( );
    int Compare(	char guessedLetters[ ], int i, char word[ ], int incorrect[ ]);
    void Asterisks(char *word, char *wordInProgress);
    void LettersGuessed ( char guessedLetters[ ] );
    void GetGuess( char guessedLetters[ ]);
    void ReplaceAsterisk(char guessedLetters[ ], char wordInProgress[ ], int i, char word[ ]);
    
    int main( )
    {
    
    	FILE *infile;
    	char word[ SIZE ];
    	char wordInProgress [ SIZE ];
    	int i;
    	char guess;
    	char guessedLetters[SIZE];
    	int numGuesses[MAX];
    	char again= 'Y';
    	int incorrect[MAX];
    	int correct;
    	
    	infile = fopen("words.txt", "r");
    	
    	fscanf( infile, " %s", word);
    	
    	Instructions( );
    	
    	
    	
    	
    	
    	if (again=='Y' || again=='y')
    	{
    		printf("Guess this word:  \n\n");
    		Asterisks(word, wordInProgress);
    		LettersGuessed ( guessedLetters );
    		GetGuess( guessedLetters);
    		Compare( guessedLetters, i, word, incorrect);
    		ReplaceAsterisk( guessedLetters, wordInProgress, i, word);
    	}
    	if (again=='Q' || again=='q')
    	{
    		printf("Thanks for playing!");
    	}
    	
    	
    	
    	
    	
    	
    	printf(" %s", wordInProgress);
    	
    	
    	
    	fclose(infile);
    	
    	
    	
    	
    	
    		return 0;
    }
    
    
    
    
    
    void Instructions ( )
    {
    	printf("WELCOME TO HANGMAN!\n\n");
    	printf("Please read the following instructions before you play.\n\n");
    	printf("-You will be presented with a word to be guessed\n");
    	printf("-Guess letters one at a time\n");
    	printf("-You can have up to six incorrect guesses\n");
    	printf("-The game will be OVER when you have guessed all the letters in the word or when you have guessed incorrectly SIX times.\n\n");
    	printf("HAVE FUN!\n\n");
    	printf("--------------------\n\n");
    }
    
    
    
    
    
    
    
    
    
    int Compare(	char guessedLetters[ ], int i, char word[ ], int incorrect[ ])
    {
    	for (i=0; i<SIZE; i++)
    	{
    		if (guessedLetters[i]==word[i])
    		{
    			printf("\nYou guessed a letter!");
    			return 0;
    		}
    	}
    
    	if (guessedLetters[i]!=word[i])
    	{
    		printf("\nThat letter is not in the word! Try again.");
    		incorrect[i]++;
    	}
    	
    	return 0;
    }
    
    
    
    
    
    
    
    
    
    
    void Asterisks(char *word, char *wordInProgress)
    {
    	int length = 0;
    	int i;
    	length=strlen(word);//counts the amount of characters in the word
    	for (i = 0; i < length; i++)
    	{
    		wordInProgress[i] = '*';//makes the asterisks array
    	}
    	printf(" %s", wordInProgress);
    }
    
    
    
    
    
    
    
    
    
    
    
    void LettersGuessed ( char guessedLetters[ ] )
    {
    	printf("\n\n");
    	printf("Letters guessed:  %s", guessedLetters);
    }
    
    
    
    
    
    
    
    
    
    void GetGuess( char guessedLetters[ ])
    {
    	printf("\nEnter a guess:  ");
    	scanf(" %s", guessedLetters);
    }
    
    
    
    
    
    
    
    
    
    void ReplaceAsterisk(char guessedLetters[ ], char wordInProgress[ ], int i, char word[ ])
    {
    	for (i = 0; i < SIZE; i++) 
    	{ 
    	if (word[i] == *guessedLetters) 
    	{ 
    	wordInProgress[i] = *guessedLetters; 
    	break; 
    	} 
    	}
    }
    
    
    
    
    
    
    
    
    char Again ( char again )
    {
    	printf("Would you like to play another game?");
    	scanf(" %c", &again);
    	return again;
    }
    
    
    
    
    
    void NumGuesses( int numGuesses )
    {
    	
    }
    There, isn't that much better? Without code tags, your code is just plain unreadable.

    It's your design, so you know far better than we do what you intend to do and what still needs to be done.

    Though in main(), shouldn't that be some way to change the value of again? And all that Compare ever returns is 0. It looks like you have a lot of code stubs waiting to be filled in, a good general approach to developing a program, but you do eventually need to fill in those stubs.

    Also, you need to test the return value of fopen in order to tell whether it succeeded or failed. You must never try to use a file that did not open successfully.
    Ok thanks for the feedback.. In response I am using xcode because I only have a macintosh at home and I the reason I posted is I am not sure where to go next and was wondering if anyone could point me in the right direction.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    question with code


    here is my code sorry guys

    Code:
    #define _CRT_SECURE_NO_DEPRECATE
    #include <stdio.h>
    #include <string.h>
    #define SIZE 20
    #define MAX 6
    
    void Instructions ( );
    int Compare(	char guessedLetters[ ], int i, char word[ ], int incorrect[ ]);
    void Asterisks(char *word, char *wordInProgress);
    void LettersGuessed ( char guessedLetters[ ] );
    void GetGuess( char guessedLetters[ ]);
    void ReplaceAsterisk(char guessedLetters[ ], char wordInProgress[ ], int i, char word[ ]);
    
    int main( )
    {
    
    FILE *infile;
    char word[ SIZE ];
    char wordInProgress [ SIZE ];
    int i;
    char guess;
    char guessedLetters[SIZE];
    int numGuesses[MAX];
    char again= 'Y';
    int incorrect[MAX];
    int correct;
    
    infile = fopen("words.txt", "r");
    
    fscanf( infile, " %s", word);
    
    Instructions( );
    
    
    
    
    
    if (again=='Y' || again=='y')
    {
    printf("Guess this word: \n\n");
    Asterisks(word, wordInProgress);
    LettersGuessed ( guessedLetters );
    GetGuess( guessedLetters);
    Compare( guessedLetters, i, word, incorrect);
    ReplaceAsterisk( guessedLetters, wordInProgress, i, word);
    }
    if (again=='Q' || again=='q')
    {
    printf("Thanks for playing!");
    }
    
    
    
    
    
    
    printf(" %s", wordInProgress);
    
    
    
    fclose(infile);
    
    
    
    
    
    return 0;
    }
    
    
    
    
    
    void Instructions ( )
    {
    printf("WELCOME TO HANGMAN!\n\n");
    printf("Please read the following instructions before you play.\n\n");
    printf("-You will be presented with a word to be guessed\n");
    printf("-Guess letters one at a time\n");
    printf("-You can have up to six incorrect guesses\n");
    printf("-The game will be OVER when you have guessed all the letters in the word or when you have guessed incorrectly SIX times.\n\n");
    printf("HAVE FUN!\n\n");
    printf("--------------------\n\n");
    }
    
    
    
    
    
    
    
    
    
    int Compare(	char guessedLetters[ ], int i, char word[ ], int incorrect[ ])
    {
    for (i=0; i<SIZE; i++)
    {
    if (guessedLetters[i]==word[i])
    {
    printf("\nYou guessed a letter!");
    return 0;
    }
    }
    
    if (guessedLetters[i]!=word[i])
    {
    printf("\nThat letter is not in the word! Try again.");
    incorrect[i]++;
    }
    
    return 0;
    }
    
    
    
    
    
    
    
    
    
    
    void Asterisks(char *word, char *wordInProgress)
    {
    int length = 0;
    int i;
    length=strlen(word);//counts the amount of characters in the word
    for (i = 0; i < length; i++)
    {
    wordInProgress[i] = '*';//makes the asterisks array
    }
    printf(" %s", wordInProgress);
    }
    
    
    
    
    
    
    
    
    
    
    
    void LettersGuessed ( char guessedLetters[ ] )
    {
    printf("\n\n");
    printf("Letters guessed: %s", guessedLetters);
    }
    
    
    
    
    
    
    
    
    
    void GetGuess( char guessedLetters[ ])
    {
    printf("\nEnter a guess: ");
    scanf(" %s", guessedLetters);
    }
    
    
    
    
    
    
    
    
    
    void ReplaceAsterisk(char guessedLetters[ ], char wordInProgress[ ], int i, char word[ ])
    {
    for (i = 0; i < SIZE; i++) 
    { 
    if (word[i] == *guessedLetters) 
    { 
    wordInProgress[i] = *guessedLetters; 
    break; 
    } 
    }
    }
    
    
    
    
    
    
    
    
    char Again ( char again )
    {
    printf("Would you like to play another game?");
    scanf(" %c", &again);
    return again;
    }
    
    
    
    
    
    void NumGuesses( int numGuesses )
    {
    
    }

IMN logo majestic logo threadwatch logo seochat tools logo