hangman game

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 8
Reputation: chrisfrolich is an unknown quantity at this point 
Solved Threads: 2
chrisfrolich chrisfrolich is offline Offline
Newbie Poster

hangman game

 
0
  #1
Jun 6th, 2008
I was wondering if programming a hangman game would be possible. I took a little bit of time to make something, but it doesn't seem to be working very well. Any info is appriciated!

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. char solution[20]; //holds solution
  7. char blank[20]; //holds "*"'s for unsolved letters
  8. int counter = 0; //general-use counter
  9. int right = 0; //1 = right guess, 0 = wrong guess.
  10. char guess;
  11.  
  12. cout<<"Enter phrase 20 chars or less."<<endl;
  13. cin.getline(solution, 20);
  14. int puzzLength = strlen(solution); //finds lengtrh of puzzle, stores INT value to puzzlength
  15.  
  16.  
  17.  
  18.  
  19. //convert puzzle to full uppercase
  20. for (counter = 0; counter < puzzLength; counter++){
  21. solution[counter] = toupper(solution[counter]);
  22. }
  23. //done converting
  24.  
  25.  
  26. strcpy_s(blank, solution); //copy solution to the 'blank' array
  27.  
  28. for (counter = 0; counter < puzzLength; counter++) { //converts characters to *'s to represent blanks
  29.  
  30. if (isalnum(solution[counter])) blank[counter] = '*';
  31. else blank[counter] = solution[counter];
  32.  
  33. } //closes for loop
  34.  
  35. while (solution != blank) { //play game until the 'blank' puzzle becomes the 'right' answer
  36.  
  37. cout<<endl<<"Solution phrase is: "<<solution<<"."<<endl;
  38. cout<<"The current 'blank' puzzle is: "<<blank<<"."<<endl;
  39. cout<<"Enter a guess."<<endl;
  40. cin>>guess;
  41. guess = toupper(guess);
  42.  
  43.  
  44. //cbeck guess!
  45. for (counter = 0; counter <= puzzLength; counter++) {
  46.  
  47. if (guess == solution[counter]) {
  48. blank[counter] = guess; //fill in the puzzle with the letter
  49.  
  50. }
  51.  
  52. } //close loop, done checking guess
  53.  
  54. } //game is over.
  55.  
  56. cout<<"Winner!";
  57. cin.get();
  58. return 0;
  59. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,978
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: hangman game

 
0
  #2
Jun 6th, 2008
Originally Posted by chrisfrolich View Post
but it doesn't seem to be working very well.
What isn't working very well? I tried the program and the only thing I found wrong is that it doesn't recognize it when you've solved the puzzle. That's because of this line:

while (solution != blank)

You're comparing two pointers to the first address of the arrays with each other. In other words: this will never be true.
Change it to:


while (strcmp(solution, blank))
And it should work a lot better


A little advice: If you use C++ why not use std::string instead of char* ?
Last edited by niek_e; Jun 6th, 2008 at 9:07 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 8
Reputation: chrisfrolich is an unknown quantity at this point 
Solved Threads: 2
chrisfrolich chrisfrolich is offline Offline
Newbie Poster

Re: hangman game

 
0
  #3
Jun 6th, 2008
that was the problem I had, I was able to get the game started and running, but the game would never recognize when it was over.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,978
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: hangman game

 
0
  #4
Jun 6th, 2008
Well if you change the line I mentioned, then that problem should be solved
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,491
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 123
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: hangman game

 
1
  #5
Jun 6th, 2008
3 Threads posting the same problem.. ? Doesn't anybody try to do their homework by themselves anymore ?? You could have all helped eachother with it and you would figured out the problem in no time.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 8
Reputation: chrisfrolich is an unknown quantity at this point 
Solved Threads: 2
chrisfrolich chrisfrolich is offline Offline
Newbie Poster

Re: hangman game

 
0
  #6
Jun 9th, 2008
yea I noticed someone else copied my code like 20 minutes after I posted this
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: 1273 | Replies: 5
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC