| | |
Hangman help help
![]() |
•
•
Join Date: Oct 2008
Posts: 101
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; #include <string> #include <cctype> void DrawGallows(int ); int main() { char solution[20]; //holds solution char blank[20]; //holds "*"'s for unsolved letters int counter = 0; //general-use counter int right = 0; //1 = right guess, 0 = wrong guess. char guess; cout<<"Enter phrase 20 chars or less."<<endl; cin.getline(solution, 20); int puzzLength = strlen(solution); //finds lengtrh of puzzle, stores INT value to puzzlength //convert puzzle to full uppercase for (counter = 0; counter < puzzLength; counter++){ solution[counter] = toupper(solution[counter]); } //done converting strcpy(blank, solution); //copy solution to the 'blank' array for (counter = 0; counter < puzzLength; counter++) { //converts characters to *'s to represent blanks if (isalnum(solution[counter])) blank[counter] = '*'; else blank[counter] = solution[counter]; } //closes for loop while (solution != blank) { //play game until the 'blank' puzzle becomes the 'right' answer cout<<"The current 'blank' puzzle is: "<<blank<<"."<<endl; cout<<"Enter a guess."<<endl; cin>>guess; guess = toupper(guess); //cbeck guess! int State =1; for (counter = 0; counter <= puzzLength; counter++) { if (guess == solution[counter]) { blank[counter] = guess; //fill in the puzzle with the letter } if (guess != solution[counter]) { State++; DrawGallows(State); } } //close loop, done checking guess } //game is over. cout<<"Winner!"; cin.get(); return 0; } void DrawGallows(int State) { if(State==6) { // The \\ will translate as '\' because it is a special char cout<<endl<<endl <<" +----+ "<<endl <<" | | "<<endl <<" | O "<<endl <<" | /|\\ "<<endl <<" | / \\ "<<endl <<" |Your Dead "<<endl <<" ============"<<endl<<endl; } else if(State==5) { cout<<endl<<endl <<" +----+ "<<endl <<" | | "<<endl <<" | O "<<endl <<" | /|\\ "<<endl <<" | \\ "<<endl <<" | "<<endl <<" ============"<<endl<<endl; } else if(State==4) { cout<<endl<<endl <<" +----+ "<<endl <<" | | "<<endl <<" | O "<<endl <<" | /|\\ "<<endl <<" | "<<endl <<" | "<<endl <<" ============="<<endl<<endl; } else if(State==3) { cout<<endl<<endl <<" +----+ "<<endl <<" | | "<<endl <<" | O "<<endl <<" | /| "<<endl <<" | "<<endl <<" | "<<endl <<" ============="<<endl<<endl; } else if(State==2) { cout<<endl<<endl <<" +----+ "<<endl <<" | | "<<endl <<" | O "<<endl <<" | | "<<endl <<" | "<<endl <<" | "<<endl <<" ============="<<endl<<endl; } else if(State==1) { cout<<endl<<endl <<" +----+ "<<endl <<" | | "<<endl <<" | "<<endl <<" | "<<endl <<" | "<<endl <<" | "<<endl <<" ============="<<endl<<endl; } }
1) WHEN I INPUT THE WRONG CHARACTER, IT RUNS UNTIL STATE =5 ,NOT AS IT SHOULD RUNS AT STATE= 1+1 = 2... HOW CAN i FIX THAT
THIS IS MY SAMPLE RUN :
•
•
•
•
Enter phrase 20 chars or less.
DSADS
Enter a guess.
D
+----+
| |
| O
| |
|
|
=============
+----+
| |
| O
| /|
|
|
=============
+----+
| |
| O
| /|\
|
|
=============
+----+
| |
| O
| /|\
| \
|
============
Enter a guess.
This
is not going to work. You cannot directly compare the strings this way. Look up use of strcmp( ) function.
Your guess checking block seems to be flawed
You are adding to the degree of hanging for every guess that does not match every position of the word. If the guess matches any of the blank spaces in the solution, player should not be further harmed.
Try using a boolean value that gets set inside the loop to a good state if the user got a character correct. Initialize it before the loop to indicate bad guess. If you enter that if block, set the value to indicate a successful guess. After the checking loop, examine that value to determine if the state variable (degree of hanging) needs to be updated.
Also, the state variable should be set to 1 before entering the main guessing loop, not inside it.
C++ Syntax (Toggle Plain Text)
while (solution != blank) { //play game until the 'blank' puzzle becomes the 'right' answer
Your guess checking block seems to be flawed
C++ Syntax (Toggle Plain Text)
//check guess! int State =1; for (counter = 0; counter <= puzzLength; counter++) { if (guess == solution[counter]) { blank[counter] = guess; //fill in the puzzle with the letter } if (guess != solution[counter]) { State++; DrawGallows(State); } } //close loop, done checking guess
You are adding to the degree of hanging for every guess that does not match every position of the word. If the guess matches any of the blank spaces in the solution, player should not be further harmed.
Try using a boolean value that gets set inside the loop to a good state if the user got a character correct. Initialize it before the loop to indicate bad guess. If you enter that if block, set the value to indicate a successful guess. After the checking loop, examine that value to determine if the state variable (degree of hanging) needs to be updated.
Also, the state variable should be set to 1 before entering the main guessing loop, not inside it.
Don't own a gun but I'm going to join the NRA.
I figure anything that irritates liberals is worth my support.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
I figure anything that irritates liberals is worth my support.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
- Writing a Hangman game (C++)
- Hangman project (VB.NET)
- Hangman Project (VB.NET)
- hangman revised (VB.NET)
- Can u help me with hangman code in vb.net please :( (VB.NET)
Other Threads in the C++ Forum
- Previous Thread: how to output integers with space!
- Next Thread: Code help
Views: 339 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm array arrays assignment beginner binary browser c++ c++borland c/c++ calculator char class classes code compile compiler constructor conversion convert count delete dll dynamic encryption error file files filestream forms fstream function functions game givemetehcodez graph graphics gui homework iamthwee input int lazy link linker list loop looping loops math matrix member memory newbie number object objects opengl operator output parameter pointer pointers problem program programming project python random read reading recursion recursive reference return server sort spoonfeeding string strings struct student studio template templates text time tree undefined variable vc++ vector video visual win32 window windows winsock wxwidgets






