4 Replies - 3213 Views - Last Post: 01 December 2009 - 09:51 PM Rate Topic: -----

#1 Ajaxx00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-November 09

Dev C++ word find program help

Post icon  Posted 01 December 2009 - 06:13 PM

Hello, I'm trying to create a program that will allow player one to enter a word and then have player 2 guess letters in the word....like hangman without the gallows. The problem I'm encountering is when player 2 guesses a letter it is not filling all characters within the word and it is returning the else statement no matter what. For example if the word is "total" and they guess a t it will fill the first t in but not the second and it will still return the else for my if statement also. I'm pretty sure the problem is within my guessing () function but I don't know what it is. I've included the two functions that might have the problem in them and main. Thanks for any advice.

 
void secret_word (char word[],char guess [],char name[])
{
	 cout << name << " enter the Secret Word (15 letters Max): ";
	 cin.width(MAX);
	 cin  >> word; cin.get();
		  
		 
		 int length = strlen(word);
												   
		 for(int i=0; i < length; ++i)
		 {
			guess[i] = '*';
			guess[i+1] = '\0';	
		 }
		 outtextxy (x1,y1,guess);
}   

int guessing (char word[],char guess[],char name[])
{
	 int length = strlen(word) - 1;
	 char letter;
	 cout << name << " its your turn. Guess a letter in the secret word.";
	 cin  >> letter; cin.get();
	 
	 
	 for (int i=0; i<length; ++i)
	 {
		 if (letter==(word[i]))
		 {   guess[i] = letter;
			 outtextxy(x1,y1,guess);
		 }		 
		 else
		 {
			 cout << "\nThere are no " << letter << "'s in the Secret Word.\n";
			 return 1;
		 }
	 }	  
} 

int main ()
{
	char player1 [MAX];
	char player2 [MAX];
	char word	[MAX];
	char guess   [MAX];	
	welcome ();
	cout << "Player 1";
	player_name (player1);
	cout << "\n\nPlayer 2";
	player_name (player2);
	cin.get ();
	board();
	secret_word (word,guess,player1);
	guessing (word,guess,player2);
	getch();
	
}
 


Is This A Good Question/Topic? 0
  • +

Replies To: Dev C++ word find program help

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: Dev C++ word find program help

Posted 01 December 2009 - 06:18 PM

You can't see the flaw in your logic here?
for (int i=0; i<length; ++i)
{
    if (letter==(word[i]))
    {   guess[i] = letter;
        outtextxy(x1,y1,guess);
    }         
    else
    {
        cout << "\nThere are no " << letter << "'s in the Secret Word.\n";
        return 1;
    }
}


Take an example word on paper and work this out. What happens if the first letter matches? What if it doesn't?
Was This Post Helpful? 0
  • +
  • -

#3 Ajaxx00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-November 09

Re: Dev C++ word find program help

Posted 01 December 2009 - 06:55 PM

[quote name='JackOfAllTrades' date='1 Dec, 2009 - 05:18 PM' post='852297']
You can't see the flaw in your logic here?
for (int i=0; i<length; ++i)
{
    if (letter==(word[i]))
    {   guess[i] = letter;
        outtextxy(x1,y1,guess);
    }         
    else
    {
        cout << "\nThere are no " << letter << "'s in the Secret Word.\n";
        return 1;
    }
}


You're right I took out the else statement and the for loop works to fill the guess array correctly. I need to return a point for an incorrect guess though...do I use a sting compare there maybe?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: Dev C++ word find program help

Posted 01 December 2009 - 08:50 PM

OK, so you've narrowed it down to the else. Think about when -- if ever -- you'd want that else to actually be executed. Surely you don't want it executed as soon as a single letter fails to match, right?
Was This Post Helpful? 0
  • +
  • -

#5 Ajaxx00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-November 09

Re: Dev C++ word find program help

Posted 01 December 2009 - 09:51 PM

Ok I now have the process working for finding the hidden word and displaying it accurately. Thanks alot for the help!! Now I'm trying to track the score, which I've never done before....I've got something wrong because it keeps returning 500.

int guessing (char word[],char guess[],char name[],char whole_word[])
{
	 settextstyle(1,0,4);
	 int num_stars = 0;
	 int length = strlen(word);
	 char letter;
	 cout << name << " its your turn. Guess a letter in the secret word.";
	 cin  >> letter; cin.get();
	 
	 
	 for (int i=0; i<length; ++i)
	 {
		 if (letter==(word[i]))
			 guess[i] = letter;
	 } 
	 outtextxy(x1,y1,guess);					   
	 for (int i=0; i<length; ++i)
	 {
		 if ('*'==(guess[i]))
				++ num_stars;
	 }
	 if (num_stars==length)
	 {
		  cout <<  "\nThere are no " << letter <<" 's in the Secret Word.\n";	   
		  return 1;
	 }
	 entire_word (word,guess,name,whole_word);
} 

int entire_word (char word[],char guess[],char name[], char whole_word []) 
{
	settextstyle(1,0,5);
	char answer;
	cout <<endl << name << " would you like to guess the word?\n(A wrong guess"
		 << "is five points and ends your turn)";
	cin  >> answer; cin.get();
	
	if (answer == 'y' || answer == 'Y')
	{
		cout <<endl << name << " what word do you think the Secret Word is?";
		cin  >> whole_word; cin.get ();
	}
	if (strcmp(whole_word,word) == 0)
	{
	   cout << "\nGOOD WORK " << name << "! You guessed the"
			<< " Secret Word.";
	   outtextxy(x1,y1,word);
	}		  
	else
	{
	   cout << "\nSorry " << name <<" " << whole_word << "  is not the"
			<< "Secret Word.";
	   return 5;
	}
}

int play_game (char word[],char guess[],char name[],char whole_word[],
int & points)
{
	points += guessing (word,guess,name,whole_word);
	score (name, points);
}

void score (char name [], int score)
{
	 cout << endl << endl << name << " has " << score << " points.\n";
}

int main ()
{
	char player1	[MAX];
	char player2	[MAX];
	char word	   [MAX];
	char guess	  [MAX];
	char whole_word [MAX];
	int player1_pts = 0;
	int player2_pts = 0;	
	welcome ();
	cout << "Player 1";
	player_name (player1);
	cout << "\n\nPlayer 2";
	player_name (player2);
	cin.get ();
	board();
	secret_word(word,guess,player1);
	play_game(word,guess,player2,whole_word,player2_pts);
	
	getch();
	
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1