| | |
stuck with hangman
![]() |
•
•
Join Date: Dec 2004
Posts: 490
Reputation:
Solved Threads: 5
Got bored, and whats better than spending your time learning. So I'd thought I'd create something fun,
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?
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { char array[5] = "John"; //chosen hangman char array2[5]; //populates with * char key; //ets a key from the user int life = 5; int correct = 0; // flag to 1 if char is in . int location = 10; //used to determine where the location is for (int i = 0 ; i < 5 ; i++) { array2[i] = '*'; //makes the * } for (int j = 0 ; j < 4 ; j++) { cout << array2[j]; //prints the *'s } while (life !=0) //if lives are greater than 0 carry on else don't { cout << "\nplease enter a guess , if you don't guess it right you loose a life" <<endl; cout << "guess is : " ; cin >>key; // grab key from user for( int guess = 0 ; guess < 5; guess++) if (array[guess] == key) { correct = 1; //if the element is found flag a 1 else flag a zero location = guess; } cout << location; if (correct ==1) { cout << "Congradulations, you've found a letter"<<endl; } else { life --; cout << "Woah, you just lost a life! , You now have only "<< life << " lives remaining" << endl; } }//while loop return 0; }
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?
•
•
Join Date: Dec 2004
Posts: 490
Reputation:
Solved Threads: 5
based from what you said, I built this:
but doesnt do the job , any other ideas?
C++ Syntax (Toggle Plain Text)
char array3[6] = "Hello"; char array4[6] = "*****"; char keycode = 'H'; { for (int k = 0; k < 5; k++) { if (array3[k] = keycode) { array3[k] = array4[k]; } } } cout << array4;
but doesnt do the job , any other ideas?
>any other ideas?
Sure, but the last idea works just fine for me:
Sure, but the last idea works just fine for me:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; class HangMan { public: HangMan ( const string& init ); public: int guess ( const char c ); bool found() const { return mask.find ( '*' ) == string::npos; } void show() const { cout<< mask <<endl; } private: string word; string mask; int guesses; }; HangMan::HangMan ( const string& init ) : word ( init ), mask ( init.length(), '*' ), guesses ( 0 ) {} int HangMan::guess ( const char c ) { string::size_type i = 0; while ( ( i = word.find ( c, i ) ) != string::npos ) { mask[i] = word[i]; ++i; } return ++guesses; } int main() { HangMan h ( "hello" ); for ( ; ; ) { char c; h.show(); cout<<"Guess a letter: "; cin.get ( c ); cin.ignore(); if ( h.guess ( c ) == 10 ) { cout<<"You lose"<<endl; break; } else if ( h.found() ) { cout<<"You win!"<<endl; break; } } }
In case you were wondering, yes, I do hate you.
•
•
Join Date: Dec 2004
Posts: 490
Reputation:
Solved Threads: 5
hat was well written, but understood none, hehe
This is what I have:
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.
This is what I have:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { char array[5] = "John"; //chosen hangman char array2[5]; //populates with * char key; //gets a character from the user int life = 5; for (int i = 0 ; i < 5 ; i++) { array2[i] = '*'; //makes the * } for (int j = 0 ; j < 4 ; j++) { cout << array2[j]; //prints the *'s } while (life !=0) //if lives are greater than 0 carry on else don't { cout << "\nplease enter a guess , if you don't guess it right you loose a life" <<endl; cout << "guess is : " ; cin >>key; // grab key from user for (int k = 0; k < 5; k++) //takes guess and checks it over { if ( key == array[k]) { array2[k] = array[k]; } } if (key!= array[0] && key!=array[1] && key!=array[2] && key!=array[3] && key!=array[5]) { life --; cout << "You've just lost a life, you now have "<< life << "remaining\n"<<endl;; } for (int h = 0; h < 4; h++) cout << array2[h] ; } if (life == 0) { cout << "Your out of lives, do you wish to play again?" <<endl; } return 0; }
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.
Your problem is here:
It's best to think of everything separated by && as a unique expression. What your code is actually doing is:
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:
C++ Syntax (Toggle Plain Text)
if (key!= array[0] && array[1] && array[2] && array[3] && array[5])
C++ Syntax (Toggle Plain Text)
if (key!= array[0] && array[1] != 0 && array[2] != 0 && array[3] != 0 && array[5] != 0)
A loop would be better anyway, especially when you move on to words with different lengths: C++ Syntax (Toggle Plain Text)
int i; for ( i = 0; i < 5; i++ ) { if ( key == array[i] ) break; } if ( i == 5 ) --life;
In case you were wondering, yes, I do hate you.
>But I'm struggling on bringing it out of the while once the word has sucessfuly been found
You can use the same trick:
You can use the same trick:
C++ Syntax (Toggle Plain Text)
for ( i = 0; i < 5; i++ ) { if ( array2[i] == '*' ) break; } if ( i == 5 ) { cout<<"You win!"<<endl; break; }
In case you were wondering, yes, I do hate you.
![]() |
Similar Threads
- Writing a Hangman game (C++)
- Hangman Game Help!! (Java)
- Homework Help - Hangman Game (VB.NET)
- Need help with a certain part to a hangman game.. (Visual Basic 4 / 5 / 6)
- Windows XP is stuck! (Windows NT / 2000 / XP)
- Can u help me with hangman code in vb.net please :( (VB.NET)
Other Threads in the C++ Forum
- Previous Thread: Using a class to add/delete/show numbers in a Link List
- Next Thread: Formating
Views: 2888 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm array arrays assignment beginner binary c++ c/c++ calculator char class classes client code command compile compiler constructor conversion convert count data delete display dll dynamic encryption error exception file files fstream function functions game givemetehcodez graph gui helpwithhomework homework http iamthwee ifstream input int integer lazy linker list loop loops math matrix member memory multidimensional newbie number object objects opengl output parameter pointer pointers problem program programming project random read recursion recursive reference simple sockets sort spoonfeeding string strings struct student studio system template templates text time tree variable vc++ vector video visual void win32 window windows winsock xml






