stuck with hangman

Reply

Join Date: Dec 2004
Posts: 490
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

stuck with hangman

 
0
  #1
Apr 2nd, 2005
Got bored, and whats better than spending your time learning. So I'd thought I'd create something fun,

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. int main()
  7. {
  8. char array[5] = "John"; //chosen hangman
  9. char array2[5]; //populates with *
  10.  
  11. char key; //ets a key from the user
  12. int life = 5;
  13. int correct = 0; // flag to 1 if char is in .
  14. int location = 10; //used to determine where the location is
  15.  
  16. for (int i = 0 ; i < 5 ; i++)
  17. {
  18. array2[i] = '*'; //makes the *
  19. }
  20.  
  21. for (int j = 0 ; j < 4 ; j++)
  22. {
  23. cout << array2[j]; //prints the *'s
  24. }
  25.  
  26. while (life !=0) //if lives are greater than 0 carry on else don't
  27. {
  28. cout << "\nplease enter a guess , if you don't guess it right you loose a life" <<endl;
  29.  
  30. cout << "guess is : " ;
  31. cin >>key; // grab key from user
  32.  
  33.  
  34. for( int guess = 0 ; guess < 5; guess++)
  35. if (array[guess] == key)
  36. {
  37. correct = 1; //if the element is found flag a 1 else flag a zero
  38. location = guess;
  39. }
  40. cout << location;
  41. if (correct ==1)
  42. {
  43. cout << "Congradulations, you've found a letter"<<endl;
  44. }
  45. else
  46. {
  47. life --;
  48. cout << "Woah, you just lost a life! , You now have only "<< life << " lives remaining" << endl;
  49. }
  50. }//while loop
  51.  
  52.  
  53.  
  54.  
  55. return 0;
  56. }

but now I've managed to dig a little hole for myself, once the user as found the chosen char, I want the '*' to be replaced by the found char... Any ideas?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,313
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 824
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: stuck with hangman

 
0
  #2
Apr 2nd, 2005
Just print array2 for each iteration of the loop. When the user chooses a letter, do a search in array, and if you find it, replace the corresponding index in array2 with the letter.
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 490
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: stuck with hangman

 
0
  #3
Apr 3rd, 2005
based from what you said, I built this:

  1.  
  2. char array3[6] = "Hello";
  3. char array4[6] = "*****";
  4. char keycode = 'H';
  5. {
  6. for (int k = 0; k < 5; k++)
  7. {
  8. if (array3[k] = keycode)
  9. {
  10. array3[k] = array4[k];
  11. }
  12. }
  13. }
  14.  
  15. cout << array4;

but doesnt do the job , any other ideas?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,313
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 824
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: stuck with hangman

 
0
  #4
Apr 3rd, 2005
>any other ideas?
Sure, but the last idea works just fine for me:
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class HangMan {
  7. public:
  8. HangMan ( const string& init );
  9. public:
  10. int guess ( const char c );
  11. bool found() const { return mask.find ( '*' ) == string::npos; }
  12. void show() const { cout<< mask <<endl; }
  13. private:
  14. string word;
  15. string mask;
  16. int guesses;
  17. };
  18.  
  19. HangMan::HangMan ( const string& init )
  20. : word ( init ), mask ( init.length(), '*' ), guesses ( 0 )
  21. {}
  22.  
  23. int HangMan::guess ( const char c )
  24. {
  25. string::size_type i = 0;
  26.  
  27. while ( ( i = word.find ( c, i ) ) != string::npos ) {
  28. mask[i] = word[i];
  29. ++i;
  30. }
  31.  
  32. return ++guesses;
  33. }
  34.  
  35. int main()
  36. {
  37. HangMan h ( "hello" );
  38.  
  39. for ( ; ; ) {
  40. char c;
  41.  
  42. h.show();
  43.  
  44. cout<<"Guess a letter: ";
  45. cin.get ( c ); cin.ignore();
  46.  
  47. if ( h.guess ( c ) == 10 ) {
  48. cout<<"You lose"<<endl;
  49. break;
  50. } else if ( h.found() ) {
  51. cout<<"You win!"<<endl;
  52. break;
  53. }
  54. }
  55. }
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 490
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: stuck with hangman

 
0
  #5
Apr 3rd, 2005
hat was well written, but understood none, hehe
This is what I have:

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. int main()
  7. {
  8. char array[5] = "John"; //chosen hangman
  9. char array2[5]; //populates with *
  10.  
  11. char key; //gets a character from the user
  12. int life = 5;
  13.  
  14. for (int i = 0 ; i < 5 ; i++)
  15. {
  16. array2[i] = '*'; //makes the *
  17. }
  18.  
  19. for (int j = 0 ; j < 4 ; j++)
  20. {
  21. cout << array2[j]; //prints the *'s
  22. }
  23.  
  24. while (life !=0) //if lives are greater than 0 carry on else don't
  25. {
  26. cout << "\nplease enter a guess , if you don't guess it right you loose a life" <<endl;
  27.  
  28. cout << "guess is : " ;
  29.  
  30. cin >>key; // grab key from user
  31.  
  32. for (int k = 0; k < 5; k++) //takes guess and checks it over
  33. {
  34. if ( key == array[k])
  35. {
  36. array2[k] = array[k];
  37. }
  38. }
  39.  
  40. if (key!= array[0] && key!=array[1] && key!=array[2] && key!=array[3] && key!=array[5])
  41. {
  42. life --;
  43. cout << "You've just lost a life, you now have "<< life << "remaining\n"<<endl;;
  44. }
  45.  
  46.  
  47. for (int h = 0; h < 4; h++)
  48. cout << array2[h] ;
  49. }
  50. if (life == 0)
  51. {
  52. cout << "Your out of lives, do you wish to play again?" <<endl;
  53. }
  54.  
  55. return 0;
  56. }

However theres no way to terminate it.... when all the letters are correct, Eg. Entering "John" will be correct letter combination but it wont terminate, not sure what to do for it.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,313
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 824
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: stuck with hangman

 
0
  #6
Apr 3rd, 2005
Your problem is here:
  1. if (key!= array[0] && array[1] && array[2] && array[3] && array[5])
It's best to think of everything separated by && as a unique expression. What your code is actually doing is:
  1. if (key!= array[0] &&
  2. array[1] != 0 &&
  3. array[2] != 0 &&
  4. array[3] != 0 &&
  5. array[5] != 0)
Of course, the array[5] appears to be a typo. It should be 4. A loop would be better anyway, especially when you move on to words with different lengths:
  1. int i;
  2.  
  3. for ( i = 0; i < 5; i++ ) {
  4. if ( key == array[i] )
  5. break;
  6. }
  7.  
  8. if ( i == 5 )
  9. --life;
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 490
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: stuck with hangman

 
0
  #7
Apr 3rd, 2005
arr cool, thanks didnt know the break function exisited! You learn something new everyday, thats the whole point of this exerice!

But I'm struggling on bringing it out of the while once the word has sucessfuly been found
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,313
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 824
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: stuck with hangman

 
0
  #8
Apr 3rd, 2005
>But I'm struggling on bringing it out of the while once the word has sucessfuly been found
You can use the same trick:
  1. for ( i = 0; i < 5; i++ ) {
  2. if ( array2[i] == '*' )
  3. break;
  4. }
  5.  
  6. if ( i == 5 ) {
  7. cout<<"You win!"<<endl;
  8. break;
  9. }
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2888 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC