Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Need help. I'm a beginner at programming. And I really want to learn more. I've created a simple hangman game. But something's not right. The program compiled properly, its just that whenever I or the user guessed all the correct letters, the program still asks the user to guess a letter instead of displaying a message saying, "Congratulations!". How can I make it display?

Here's the code:


C++
#include "stdafx.h"
#include <stdio.h>   
#include <conio.h>
#include <string.h>  
#include <ctype.h>  
#include <stdlib.h> 
#include <time.h>
#include "iFunction.h"

#pragma warning(disable:4996)

#define NUM_OF_WORDS   25 // The number of words in the words list
#define NUM_OF_CHANCES 5  // The number of tries allowed
#define TRUE           1  // Bool for TRUE
#define FALSE          0  // Bool for FALSE


int _tmain(int argc, _TCHAR* argv[])
{
		// Hang Man Figure
		char *hanged[]={
						 "\t\t\t\t|=====|\n"
						 "\t\t\t\t      |\n"
						 "\t4 lives left...\t\t      |\n"
						 "\t\t\t\t      |\n"
						 "\t\t\t\t     ===\n",
						 "\t\t\t\t|=====|\n"
						 "\t\t\t\tO     |\n"
						 "\t3 lives left...\t\t      |\n"
						 "\t\t\t\t      |\n"
						 "\t\t\t\t     ===\n",
						 "\t\t\t\t|=====|\n"
						 "\t\t\t\tO     |\n"
						 "\t2 lives left...\t\t|     |\n"
						 "\t\t\t\t      |\n"
						 "\t\t\t\t     ===\n",
						 "\t\t\t\t |=====|\n"
						 "\t\t\t\t O     |\n"
						 "\t1 life left...\t\t/|\\    |\n"
						 "\t\t\t\t |     |\n"
						 "\t\t\t\t     ===\n",
						 "\t\t\t\t |=====|\n"
						 "\t\t\t\t[X]    |\n"
						 "\tNo lives left...\t/|\\    |\n"
						 "\tYou Got Hanged!\t\t/ \\    |\n"
						 "\t\t\t\t     ===\n"

		};
		// set of words to be guessed by the user(s)
		char *words[] = {
						  "ARGUMENTS", "BYPASSING", "CHROMATIC", "DOWNLOADING","ELECTRICAL","FREEFLOW","GOOGLE","HACKING", "INTRANET", "JUNKFILE", "KEYWORDS", "LIFETIME",
						  "MOBILE", "NULLIFY", "PASSWORD", "QUADCORE", "RECONNECT", "STRUCTURE", "TIMELINES", "UPLOADING", "VOICECOMMAND", "WIRELESS", "XENDER",
						  "YOUTUBE", "ZIPCODES"
		};
		
		srand((unsigned) time(NULL));
		unsigned short chosen_word = rand() % NUM_OF_WORDS; // the one who will choose the random words above
		char guessed;
		size_t len = strlen(words[chosen_word]); //who count each letter of the choosen word to be guessed
		size_t i = 0;
		unsigned int found = FALSE;
		unsigned int chances = 0; //this will serve as counting value of the incorrect letters
		char repeat;

	do
	{
		system("cls");
		chances = 0; //chances reset to 0
		chosen_word = rand() % NUM_OF_WORDS; //responsible for getting another word randomly*
		len = strlen(words[chosen_word]); //counters*
		i = 0;

		ShowHeadingRules();
		ShowWordClue(len);
		char *a = (char *)malloc(len + 1);
		printf("\n Guess a letter: ");
		
		while(1)//1 bec. the loop is TRUE
		{
			guessed = toupper(getchar());

			if(guessed != '\n')
			{
				if (found = len) //correct guessed
				{
					if(strcmp(a, words[chosen_word]) == TRUE)
					{	
						Load();
						system("cls");
						printf("\n Word to be guessed: ");
						SeeInWord(guessed, words[chosen_word], a, len, &found);
						printf("\n\n Guess a letter: ");
					}

				}
			}

			else if(found != TRUE) //incorrect guess
			{
				system("cls");
				if (chances < 4)
				{
					printf("\n Word to be guessed: ");
					SeeInWord(guessed, words[chosen_word], a, len, &found); // to show every letters of the correct answers
				}
				

				
				if(chances >= 4)
				{
					system("cls");

					printf("\n The Exact Word is '%s'", words[chosen_word]); // to show the correct word
					printf("\n\n%s\n\n", hanged[chances++]);  // show the Hanged Figure
					
					if(chances == 5)
					{
						GameOver(); //ending message
						printf("\n\n\n\n\n");
						printf(" ==========================================================================\n");
						printf(" To repeat the program, press [y] or [Y]..\n");
						printf(" If you wish to terminate the program,\n");
						printf(" Just press any key: ");
						repeat = getch();
						break;
					}
				}

				
				// tells the user that he/she inputs an incorrect letters
				printf("\n\n\n\t\t\t********************");		
				printf("\n\t\t\t*       Uy!!!      *");
				printf("\n\t\t\t*Tag-ana ug tarong!*");
				printf("\n\t\t\t********************");
				printf("\n --------------------------------------------------------------------");
				printf("\n\n%s\n", hanged[chances++]);
				printf(" --------------------------------------------------------------------");
				printf("\n Guess a letter: ");
				
			}
			
			
		}
	}while(repeat == 'y' || repeat == 'Y'); //to repeat the program  "NOTED: there's a glitch" ^_^

	getch();
	return 0;
}


//Header file
C++
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#pragma warning(disable:4996)

//Prototypes
void ShowHeadingRules();
void GameOver();
void SeeInWord(char, const char [], char *, const size_t, unsigned int *);
void ShowWordClue(const size_t);
void Load();



void ShowHeadingRules() //Shows the game heading and prints out the rules of the game
{
	printf("\n\t\t\t******************************");
	printf("\n\t\t\t*PROGRAMMER: Marvin M. Maasin*");
	printf("\n\t\t\t*INSTRUCTOR: Sammy L. Bastes *");
	printf("\n\t\t\t******************************\n\n");
    printf("\t\t--------------------------------------------\n");
	printf("\t\t| #  #   #   #   #  ###  #   #   #   #   # |\n");
	printf("\t\t| #  #  # #  ##  # #     ## ##  # #  ##  # |\n");
	printf("\t\t| #### ##### # # # #  ## # # # ##### # # # |\n");
	printf("\t\t| #  # #   # #  ## #   # #   # #   # #  ## |\n");
	printf("\t\t| #  # #   # #   #  ###  #   # #   # #   # |\n");
	printf("\t\t--------------------------------------------\n");
	printf("******************************************************************************\n");
    printf("*Hangman is a game where you are expected to guess a word in a certain number*\n");
    printf("*of chances that is before you hang the hangman.                             *\n");
	printf("******************************************************************************\n\n");
    printf("A word has been choosen, begin guessing it. Good luck!!!\n\n");
    printf("You have to guess this word:");
}

void GameOver() //Ending Message
{
	printf("\t[][][][][][][][][][][][][][][][][][][][][]\n");
	printf("\t[]                                      []\n");
    printf("\t[]   You killed a person! Good Bye. :'( []\n");
	printf("\t[]                                      []\n");
	printf("\t[][][][][][][][][][][][][][][][][][][][][]");
}

void ShowWordClue(const size_t len) //Flow of the loop
{
    size_t i; // Loop counter
    for(i = 0; i < len; i++)
	{
        printf(" _ ");
	}
    printf("\n\n");
}



void SeeInWord(char guessed, const char word[], char *a, const size_t len, unsigned int *found) // Used as counter
{
    size_t i;


    for(i = 0; i < len; i++)
    {
        if(guessed == word[i])
        {
            *(a + i) = guessed;
            *found = TRUE;
        }
        else
        {
            // To identify letters in the array

            if(*(a + i) >= 65 && *(a + i) <= 90 ); // Do not store '_' if there is a Capital letter in that location
            else if(*(a + i) >= 97 && *(a + i) <= 122); // Do not store '_' if there is a Small letter in that location
            else *(a + i) = '_'; // Else store an underscore.
        }
    }

    for(i = 0; i < len; i++)// Print the 'a' array where each charachter printed is followed by a white space.
    {
        printf("%c ", *(a + i));
    }
	for(i = 0; i < len; i++)
	{
		if(strcmp(a, word) == 0)
		{
			printf("\nCONGRATULATIONS! ");
		}
		
	}
}

void Load() //Loading Process
{
	for (int p = 0; p < 8; p++)
	{
		switch (p)
		{
		case 1:
			system("cls");
			printf("\n\n\n\n\t\t\t Loading ------ [10%%]");
		case 2:
			system("cls");
			printf("\n\n\n\n\n");
			printf("\t\t\t  Loading ---    [20%%]");
		case 3:
			system("cls");
			printf("\n\n\n\n\n");
			printf("\t\t\t  Loading ------ [30%%]");
		case 4:
			system("cls");
			printf("\n\n\n\n\n");
			printf("\t\t\t  Loading ----   [40%%]");
		case 5:
			system("cls");
			printf("\n\n\n\n\n");
			printf("\t\t\t  Loading ------ [50%%]");
		case 6:
			system("cls");
			printf("\n\n\n\n\n");
			printf("\t\t\t  Loading ---    [60%%]");
		case 7:
			system("cls");
			printf("\n\n\n\n\n");
			printf("\t\t\t  Loading ------ [70%%]");
		case 8:
			system("cls");
			printf("\n\n\n\n\n");
			printf("\t\t\t  Loading ---    [80%%]");
		case 9:
			system("cls");
			printf("\n\n\n\n\n");
			printf("\t\t\t  Loading ------ [90%%]");
		case 10:
			system("cls");
			printf("\n\n\n\n\n");
			printf("\t\t\t  Loading ---    [100%%]");
		break;
		}
	}
}


What I have tried:

I just tried to make a simple hangman game in C language.
Posted
Updated 4-Aug-16 8:24am

C++
if(strcmp(a, words[chosen_word]) == TRUE)

This is not correct; strcmp returns 0 when the strings match. However the constant TRUE is equal to 1, soy you will only run the following block when the strings do not match.

Note: I have not gone through every line of your program as it would just take too long. You should run some tests in the debugger to try and see where you are taking the wrong path.
 
Share this answer
 
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900