• 0

[C++] basic hangman game


Question

Hi all;

Im taking programming language © in my uni and as practice I my professor told us to make a hangman to practice for the next lab.

I have the idea of how to program it but my code seems to have an error. I cant find where it is and Dev-C++ debugger doesnt help so much.

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

char word[20] = {0};
char newword[20]={0};

int main()
{
int lifes = 0;
char letter;
int i =0;
int sentinel =0;

	printf("write a word \n");
	scanf("%s",word);

	while ( lifes <=5 && newword != word)
	{

	printf("write a letter: \n ");
	getche();
	letter = getche();
	printf("\n%c",letter);
	sentinel =0;
	for (i=0;i<20;i++){
		if (letter == word[i])
		   newword[i] = letter;

		else
		sentinel++;

		}

		if (sentinel == 20)
		lifes++;

}
if (lifes>5)
		   printf("you lose\n");
else if (newword == word) 
	  printf("you win\n");


	system("pause");

	return 0;
}

do I have to create a function to compare both arrays?

thanks in advance.

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

you can safely remove the first getche() call. I'm not sure if using this before the assignment removes the char you type from the buffer or not.

printf("write a letter: \n ");
	getche();
	letter = getche();

So change this to

printf("write a letter: \n ");
	letter = getche();

I hope this helps some, but what is the problem you are encountering with your program? Does getche not grab the current char you enter?

Wesley

*EDIT*

read over the code to fast, removing a few changes

Link to comment
Share on other sites

  • 0

Thank you all for the replies.

The problem was the comparation of the arrays.

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int compare(char a[],char b[]);
char word[20] = {0};
char newword[20]={0};

int main()
{
int lifes = 0;
char letter;
int i =0;
int sentinel =0;

	printf("write a word \n");
	scanf("%s",word);

	while ( lifes <=5 && !(compare(newword,word)))
	{

	printf("\n lifes: %d write a letter: \n ",(6-lifes));
	letter = getche();
	//printf("\n%c",letter);
	sentinel =0;
	for (i=0;i<20;i++){
		if (letter == word[i])
		   newword[i] = letter;

		else
		sentinel++;

		}

		if (sentinel == 20)
		{
		lifes++;
		sentinel =0;
	}	

}
if (lifes>5)
		   printf("you lose\n");
else if (compare(newword,word))
	  printf("\n you win\n");


	system("pause");

	return 0;
}

int compare(char a[],char b[])
{
	int i;
	for (i=0;i<20;i++){
	if (a[i] != b[i])
		return 0;

	}	
	return 1;
}

Link to comment
Share on other sites

  • 0
Why not strcmp or strncmp? (see my earlier post)

I tried to use it but I get "'strcmp' makes pointer from integer without a cast"

Do I have to know about pointers to use that function? We are going to do pointers this friday.

Link to comment
Share on other sites

  • 0
Do I have to know about pointers to use that function? We are going to do pointers this friday.

No, you don't; if you get that warning, then your syntax is incorrect; with word and newword defined as char arrays, strcmp(word, newword) should not give any warnings or errors.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.