4 Replies - 290 Views - Last Post: 05 August 2012 - 10:11 PM Rate Topic: ***-- 1 Votes

#1 Anafam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-August 12

small bug in a guessing game

Posted 05 August 2012 - 03:18 PM

this is a small code from a Hangman game coded in "C" which I'm working on.
It has a small undetectable BUG :notify:

When I enter a letter to guess the word it should not give me
"wrong guess" when that letter is inside the word. for example
when i enter "f" for facebook it works correctly BUT it
prints "wrong guess" which it shoudn't....THANKING YOU a million times
for the help :)

PS: This is just one instance of the program soo don't worry about the
rest of the coding, i just need help in this step.


#include "conio.h"
#include "stdio.h"
#include "string.h"
void main()
{
	clrscr();
	char word[25]= "facebook";
	char guess;
	int i;
	int blank=0;
	int word_length = strlen(word);

	printf("%s\n",word);
	while(blank<word_length)
	{
	  printf("\x7f"); /*prints an ASCII character*/ 
	  blank++;
	}

	printf("\n\nenter a character to guess ");
	scanf("%c",&guess);
	for(i=0;i<word_length;i++)
	{
	  if(word[i]==guess)
	  {
	  gotoxy(i+1,2);
	  printf("%c",guess);
	  }

	    else if(word[i] != guess)
	    {
	    gotoxy(1,7);
	    printf("wrong guess");
	    }
	}
	getch();
}


Is This A Good Question/Topic? 0
  • +

Replies To: small bug in a guessing game

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2503
  • View blog
  • Posts: 8,564
  • Joined: 08-August 08

Re: small bug in a guessing game

Posted 05 August 2012 - 03:33 PM

You're checking to see if guess matches every character in word. It won't! Write a separate function that returns true if guess is anywhere in word and false if it is not.
Was This Post Helpful? 0
  • +
  • -

#3 Anafam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-August 12

Re: small bug in a guessing game

Posted 05 August 2012 - 04:08 PM

View PostCTphpnwb, on 05 August 2012 - 03:33 PM, said:

You're checking to see if guess matches every character in word. It won't! Write a separate function that returns true if guess is anywhere in word and false if it is not.


I'm a beginner at C so I don't have much experience. I made a separate function to do the work like u said. It works but its still has the same problem...
I would be very grateful if you could make a function for me :)

#include "conio.h"
#include "stdio.h"
#include "string.h"

char word[25]= "facebook";
void function(char,int);
void main()
{
	clrscr();
	char guess;

	int blank=0;
	int word_length = strlen(word);

	printf("%s\n",word);
	while(blank<word_length)
	{
	  printf("\x7f"); /*prints an ASCII character*/
	  blank++;
	}

	printf("\n\nenter a character to guess ");
	scanf("%c",&guess);
	function(guess,word_length);

	getch();
}

	void function(char g,int len)
{
	int i;
	for(i=0;i<len;i++)
	{
	  if(word[i]==g)
	  {
	  gotoxy(i+1,2);
	  printf("%c",g);
	  }

	    else if(word[i] != g)
	    {
	    gotoxy(1,7);
	    printf("wrong guess");
	    }
	}
}

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2503
  • View blog
  • Posts: 8,564
  • Joined: 08-August 08

Re: small bug in a guessing game

Posted 05 August 2012 - 04:36 PM

The function should not be void and its name should be descriptive:
int is_in_word( char guessed, char wd[], int wd_size) {
// if guessed character is in the wd array, return 1, otherwise return 0
}


This post has been edited by CTphpnwb: 05 August 2012 - 04:45 PM

Was This Post Helpful? 0
  • +
  • -

#5 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1418
  • View blog
  • Posts: 2,681
  • Joined: 30-May 10

Re: small bug in a guessing game

Posted 05 August 2012 - 10:11 PM

Cross-posted for reference
http://forum.codecal...-guessing-game/

Tips on netiquette
http://www.catb.org/...ions.html#forum
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1