| | |
Hangman help help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
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.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
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
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






